JS将数组转换为json链表?

时间:2016-10-06 13:36:44

标签: javascript jquery json

我是JS的新手,组织数据的概念让我有所收获,尝试从特定的数组格式中获取数据(因为这是我必须使用的)并将其输出为另一种特定的JSON格式。

这是为了将数据传递给D3 sankey模块 https://github.com/d3/d3-plugins/blob/master/sankey/sankey.js

我无法弄清楚如何将节点的索引添加到链接中,而不是名称。

真的,我完全迷失了它! 我在这里做了一个小提琴: https://jsfiddle.net/adamdavi3s/kw3jtzx4/

以下是数据和所需输出的示例

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

这是来自小提琴的代码

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);

2 个答案:

答案 0 :(得分:5)

在javascript中:

[]注释用于描述数组,如:

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

{}用于描述对象

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

您可以在彼此内部使用

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

试试这个:

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.net/9x4rdyy7/

答案 1 :(得分:4)

Array.reduce()非常适合此用例;)

看看。

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);