gpt4 book ai didi

javascript - 如何根据其公共(public)值之一对每个级别的 json 数据进行分组

转载 作者:行者123 更新时间:2023-11-30 20:06:55 25 4
gpt4 key购买 nike

我在尝试使用 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));

但是它只是将根节点转换为键值对。

任何建议都会有所帮助。谢谢

最佳答案

您可以使用迭代(对于实际数组)和递归(对于子数组)方法按所需键对每个数组进行分组。

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

关于javascript - 如何根据其公共(public)值之一对每个级别的 json 数据进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52829459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com