Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

data maping facing issue to conver data given below

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
     })
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading