Ad

Data Maping Facing Issue To Conver Data Given Below

- 1 answer

hi everyone i have data given below

nodes data

var nodes=[
  {
    name:'shanu',
    value:5
  },
  {
    name:'bhanu',
    value:2
  },
  {
    name:aaditya,
    value:1
  }
]

edge data

var edge =[
  {
    source:'shanu',
    target:'aaditya'
  },
  {
    source:'shanu',
    target:'bhanu'
  },
  {
    source:'aaditya',
    target:'bhanu'
  }
]

Now I just want to convert edge data source and target based on id

output I want from above data

var convertedEdge=[
  {
    source:0,
    target:2
  },
  {
    source:0,
    target:1
  },
  {
    source:1,
    target:2
  }
]
Ad

Answer

you need to use two loops:

var convertedEdge = [], source = '', target = '';
for (i in edge) {
   source = edge[i].source;
   target = edge[i].target;
   for(j in nodes) {
        if (source == nodes[j].name) {
            source = j
          }
        if (target == nodes[j].name) {
           target = j
          }
   }
     convertedEdge.push({
       source: source,
       target: target
     })
}
Ad
source: stackoverflow.com
Ad