最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS converting an array to a json linked list? - Stack Overflow

programmeradmin0浏览0评论

I am new to JS and the concepts of organising data elude me a little, trying to take data from a specific array format (as this is what I have to work with) and output it into another specific JSON format.

This is to pass data to the D3 sankey module .js

I can't figure out is how to add the index of the node into the links, rather than the name.

Really I am just totally lost with it! I made a fiddle here: /

Below is an example of the data and the output required

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 code from the fiddle thusfar

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) {
      // Element was not found, add it.
    sourceArray.push(node);
}

}
console.log(sourceArray);

I am new to JS and the concepts of organising data elude me a little, trying to take data from a specific array format (as this is what I have to work with) and output it into another specific JSON format.

This is to pass data to the D3 sankey module https://github./d3/d3-plugins/blob/master/sankey/sankey.js

I can't figure out is how to add the index of the node into the links, rather than the name.

Really I am just totally lost with it! I made a fiddle here: https://jsfiddle/adamdavi3s/kw3jtzx4/

Below is an example of the data and the output required

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 code from the fiddle thusfar

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) {
      // Element was not found, add it.
    sourceArray.push(node);
}

}
console.log(sourceArray);
Share Improve this question edited Oct 6, 2016 at 13:57 Adam Davies asked Oct 6, 2016 at 13:36 Adam DaviesAdam Davies 1552 silver badges8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

In javascript:

[ ] annotations are used to describe an Array, like:

var names=["John","Lisa"]

{ } Its are used to describe an Object

var person = {"name" : "John", "age" : 23}

You can use them inside one another

 var people=[{"name" : "John", "age" : 23},{"name" : "Lisa", "age" : 44}]

Try this:

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=[];
var linkArray=[];

for (var i=0; i <data.length; i++ )		{
var node= {"name":data[i].source,};
var link= {
"source":i,
"target":data[i].target,
"value":data[i].value,
};
var found = jQuery.inArray(node, sourceArray);
if (found >= 0) {
    // Element was found, remove it.
    sourceArray.splice(found, 1);
    linkArray.splice(found, 1);
} else {
    // Element was not found, add it.
    sourceArray.push(node);
    linkArray.push(link);
}

}
finalArray={"nodes": sourceArray,"links": linkArray}
console.log(finalArray);

https://jsfiddle/9x4rdyy7/

Array.reduce() is perfect for this use case ;)

Take a look.

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 = data.reduce(function(result, item){
  for(key in search = ['source','target']) {
    var value = item[search[key]];
    
    if(! result.index.hasOwnProperty(value)){
       result.index[value] = Object.keys(result.index).length;
       result.nodes.push({name: value});
    }
  }
  
  result.links.push({
    source: result.index[item.source],
    target: result.index[item.target],
    value: Number(item.value)
  });
  
  return result;
}, {nodes: [], links: [], index: {}});

delete output.index;
console.log(output);

发布评论

评论列表(0)

  1. 暂无评论