我在我的项目中遇到了一些问题,其中我试图使用JSON数据来构建树
var treeData = [
{
"name": "Root Node",
"parent": "null",
"children": [
{
"name": " Transport",
"parent": "Root Node",
"nodeId":"nod411",
"children": [
{
"name": "Cars",
"parent": "Transport",
"nodeId":"nodjz13l"
},
{
"name": "Transport-2",
"parent": "Transport",
"nodeId":"nod411",
"children": [
{
"name": "Cars",
"parent": "Transport-2",
"nodeId":"nod1_cdot"
},
{
"name": "Cars",
"parent": "Transport-2",
"nodeId":"nod45jkl"
},
{
"name": "Bikes",
"parent": "Transport-2",
"nodeId":"nod411"
},
{
"name": "Cars",
"parent": "Transport-2",
"nodeId":"nod961"
},
{
"name": "Cars",
"parent": "Transport-2",
"nodeId":"nodm2m40"
},
{
"name": "Bikes",
"parent": "Transport-2",
"nodeId":"nod411"
},
]
},
{
"name": "Cars",
"parent": "Transport",
"nodeId":"nod411"
},
{
"name": "Cars",
"parent": "Transport",
"nodeId":"nod311"
},
{
"name": "Bikes",
"parent": "Transport",
"nodeId":"nod411"
},
]
},
{
"name": "Bikes",
"parent": "Root Node",
"nodeId":"nod411"
}
]
}
];
在这里,我想根据类似键值的键将相似条目分组。
var treeData = [
{
"name": "Root Node",
"parent": "null",
"children": [
{
"name": " Transport",
"parent": "Root Node",
"nodeId":"nod411",
"children": [
{"key":"car","values":[
{
"name": "Cars",
"parent": "Transport",
"nodeId":"nodjz13l"
},
{
"name": "Cars",
"parent": "Transport",
"nodeId":"nod411"
},
{
"name": "Cars",
"parent": "Transport",
"nodeId":"nod311"
}
]
},
{
"name": "Transport-2",
"parent": "Transport",
"nodeId":"nod411",
"children": [
{"key":"car","values":[
{
"name": "Cars",
"parent": "Transport-2",
"nodeId":"nod1_cdot"
},
{
"name": "Cars",
"parent": "Transport-2",
"nodeId":"nod45jkl"
},
{
"name": "Cars",
"parent": "Transport-2",
"nodeId":"nod961"
},
{
"name": "Cars",
"parent": "Transport-2",
"nodeId":"nodm2m40"
}
]
},
{"key":"Bikes","values":[
{
"name": "Bikes",
"parent": "Transport-2",
"nodeId":"nod411"
},
{
"name": "Bikes",
"parent": "Transport-2",
"nodeId":"nod411"
}
]
}
]
},
{"key":"Bikes","values":[
{
"name": "Bikes",
"parent": "Transport",
"nodeId":"nod411"
}
]
}
]
},
{
"name": "Bikes",
"parent": "Root Node",
"nodeId":"nod411"
}
]
}
];
我想在树的每个级别对数据进行分组。
我尝试使用d3作为嵌套
var treeDataByName = d3.nest()
.key(function(d) { return d.name; })
.entries(treeData);
console.log(JSON.stringify(treeDataByName));
但是,它仅将根节点转换为键值对。
任何建议都会有所帮助。 谢谢
答案 0 :(得分:1)
您可以使用迭代(针对实际数组)和递归(带有子项)方法,根据所需键将每个数组分组。
function groupBy(array, key) {
return array.reduce((r, o) => {
var temp = r.find(g => g.key === o[key]),
value = Object.assign({}, o, o.children && { children: groupBy(o.children, key) });
if (temp) {
temp.values.push(value);
} else {
r.push({ key: o[key], values: [value] });
}
return r;
}, []);
}
var tree = [{ name: "Root Node", parent: "null", children: [{ name: " Transport", parent: "Root Node", nodeId: "nod411", children: [{ name: "Cars", parent: "Transport", nodeId: "nodjz13l" }, { name: "Transport-2", parent: "Transport", nodeId: "nod411", children: [{ name: "Cars", parent: "Transport-2", nodeId: "nod1_cdot" }, { name: "Cars", parent: "Transport-2", nodeId: "nod45jkl" }, { name: "Bikes", parent: "Transport-2", nodeId: "nod411" }, { name: "Cars", parent: "Transport-2", nodeId: "nod961" }, { name: "Cars", parent: "Transport-2", nodeId: "nodm2m40" }, { name: "Bikes", parent: "Transport-2", nodeId: "nod411" }] }, { name: "Cars", parent: "Transport", nodeId: "nod411" }, { name: "Cars", parent: "Transport", nodeId: "nod311" }, { name: "Bikes", parent: "Transport", nodeId: "nod411" }] }, { name: "Bikes", parent: "Root Node", nodeId: "nod411" }] }],
result = groupBy(tree, 'name');
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }