Links in O(n) or O(1)

Hi I made the graph data structures in React and D3js. Spent 3 hours writing the helper function for the links(edges) of the graph so d3 could draw it. Anyone wanna try and see if I can memoize or make the helper function O(n )

const graph = {
  a:['b','c'],
  b:['d','e'],
  c:['d','f'],
  d:['f'],
  e:['b','c'],
  f:[]
}
const nodesfx = (data) => {
  const Nodes = data.map( (d,i) => {
    const n ={}
    n['id'] = i
    n['name'] = d
    return n
  })
  return Nodes
}
const linksfx = (data, node) => {
  let Links = {}
  const arr = []
  for(let k of Object.keys(data)){
    for(let id of Object.keys(node)){
   
      if(data[k].length > 0){
        if(k === node[id].name){
          Links['source'] = id
         
        }
        data[k].map(d =>{
           if(d === node[id].name){
            Links['target'] = id
            console.log('hi there', Links.source, Links.target)
            arr.push({source: +Links['source'],target:  +Links['target']})
           }
       })
      }
    }  
  }
  return arr
}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.