Home > database >  data maping facing issue to conver data given below
data maping facing issue to conver data given below

Time:02-20

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
  }
]

CodePudding user response:

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
     })
}
  • Related