I am new to JS, and the concepts of organizing data elude me a bit, trying to take data from a specific array format (since this is what I should work with) and output it to another specific JSON format.
This is data transfer to the D3 sankey module https://github.com/d3/d3-plugins/blob/master/sankey/sankey.js
I cannot figure out how to add the node index to the links, not the name.
In fact, I'm just completely lost! I made a fiddle here: https://jsfiddle.net/adamdavi3s/kw3jtzx4/
Below is an example of the data and the desired result.
var data= [ {"source":"Agricultural 'waste'","target":"Bio-conversion","value":"124.2729"}, {"source":"Bio-conversion","target":"Electricity grid","value":"0.597"}, {"source":"Bio-conversion","target":"Losses","value":"26.862"}, {"source":"Bio-conversion","target":"Liquid","value":"280.322"}, {"source":"Losses","target":"Liquid","value":"280.322"} ]; var output= { "nodes":[ {"name":"Agricultural 'waste'"}, {"name":"Bio-conversion"}, {"name":"Electricity grid"}, {"name":"Losses"}, {"name":"Liquid"} ], "links":[ {"source":0,"target":1,"value":124.729}, {"source":1,"target":2,"value":0.597}, {"source":1,"target":3,"value":26.862}, {"source":1,"target":4,"value":280.322}, {"source":3,"target":4,"value":280.322} ] };
Here is my fiddle code, so
var data=[{"source":"Agricultural 'waste'","target":"Bio-conversion","value":"124.2729"}, {"source":"Bio-conversion","target":"Electricity grid","value":"0.597"}, {"source":"Bio-conversion","target":"Losses","value":"26.862"}, {"source":"Bio-conversion","target":"Liquid","value":"280.322"}, {"source":"Losses","target":"Liquid","value":"280.322"} ]; var sourceArray=[]; for (var i=0; i <data.length; i++ ) { var node= {"name":data[i].source}; var found = jQuery.inArray(node, sourceArray); if (found < 0) {