我正在使用此数据输入来创建D3.js可视化:
好的,现在我的数据输入是我已硬编码的json文件:
{
"name":"Fun",
"children": [
{
"name": "Legal",
"children": [
{ "name": "Adventure" },
{ "name": "Movie" },
{ "name": "M&m" }
]
},
{
"name": "frowned upon",
"children": [
{ "name": "recreational stuff" },
{ "name": "religious views" }
]
}
]
}
但是我的数据输入实际上是:
答案 0 :(得分:3)
您可以编写递归函数来重组数据。
Restangular
答案 1 :(得分:1)
var test = new function() {
var table = [
['fun', 'legal', 'adventure'],
['fun', 'legal', 'mvie'],
['fun', 'legal', 'M&M'],
['fun', 'Frowned upon', 'Rec stuff'],
['fun', 'Frowned upon', 'Regligious views']
];
var res = [];
this.init = function() {
for (var i = 0; i < table.length; i++) {
var curRow = table[i];
test.myAddFun(res, curRow, 0);
}
console.log(res);
};
this.myAddFun = function(_res, arr, startIndex) {
var addedToJSON = false;
for (var i = 0; i < _res.length; i++) {
var curJSON = _res[i];
if (curJSON['name'] == arr[startIndex]) {
if (startIndex < arr.length - 1) {
test.myAddFun(curJSON['children'], arr, startIndex + 1);
}
addedToJSON = true;
break;
}
}
if (addedToJSON) {
return;
}
var curJSON = {};
curJSON['name'] = arr[startIndex];
if (startIndex < arr.length - 1) {
curJSON['children'] = [];
test.myAddFun(curJSON['children'], arr, startIndex + 1);
}
_res.push(curJSON);
return;
};
};
test.init();