从平面数组创建嵌套对象 [英] Create nested object from flat array
问题描述
我目前有一个对象数组,我正在尝试使用ID
作为对象键将其整形为嵌套对象,如果不是"0"
,则使用parent
id定位该ID
.我尝试了几种方法,但是我在努力工作!对我而言,主要绊脚石是超过一到两层的任何东西.理想情况下,我需要使它具有动态性,以便它可以处理任何深度,尽管在野外我怀疑它会超过3层.
I currently have an array of objects which I'm trying to reshape into a nested object with the ID
as an object key, and target that ID
with the parent
id if it's not "0"
. I've tried several approaches but I'm struggling big time! The main stumbling block for me is anything beyond one or two layers deep. Ideally I need this to be dynamic so it can handle potentially any depth although in the wild I doubt it will go beyond 3 layers.
这是传入的对象数组:
[
{
ID: "1671",
parent: "0",
},
{
ID: "1223",
parent: "0",
},
{
ID: "1668",
parent: "0",
},
{
ID: "1688",
parent: "0",
},
{
ID: "1669",
parent: "0",
},
{
ID: "1681",
parent: "1669",
},
{
ID: "1680",
parent: "1669",
},
{
ID: "1670",
parent: "1669",
},
{
ID: "1682",
parent: "1669",
},
{
ID: "1433",
parent: "1682",
},
{
ID: "1684",
parent: "1682",
},
{
ID: "1672",
parent: "1684",
},
{
ID: "1685",
parent: "1672",
},
{
ID: "1686",
parent: "1672",
},
{
ID: "1683",
parent: "0",
},
{
ID: "1230",
parent: "0",
},
{
ID: "1667",
parent: "0",
},
{
ID: "1687",
parent: "0",
}
];
这是所需的转换:
1671: {
ID: "1671",
parent: "0",
},
1223: {
ID: "1223",
parent: "0",
},
1668: {
ID: "1668",
parent: "0",
},
1688: {
ID: "1688",
parent: "0",
},
1669: {
ID: "1669",
parent: "0",
children: {
1681: {
ID: "1681",
parent: "1669",
},
1680: {
ID: "1680",
parent: "1669",
},
1670: {
ID: "1670",
parent: "1669",
},
1682: {
ID: "1682",
parent: "1669",
children: {
1433: {
ID: "1433",
parent: "1682",
},
1684: {
ID: "1684",
parent: "1682",
children: {
1672: {
ID: "1672",
parent: "1684",
children: {
1685: {
ID: "1685",
parent: "1672",
},
1686: {
ID: "1686",
parent: "1672",
}
}
}
}
}
}
}
}
},
1683: {
ID: "1683",
parent: "0",
},
1230: {
ID: "1230",
parent: "0",
},
1667: {
ID: "1667",
parent: "0",
},
1687: {
ID: "1687",
parent: "0",
}
我的大多数失败方法都一直使用Array.reduce()
和其他类型的递归,但无济于事,所以我很想知道是否有解决方案.
Most of my failed approaches have been using Array.reduce()
and other types of recursion but to no avail, so I'm interested to see if there is a solution for this.
谢谢
推荐答案
您可以使用以下递归函数:
You can use recursive function like this:
function findFor(parentId) {
// create a new object to store the result
var z = {};
// for each item in a
for (var i = 0; i<a.length; i++){
// find all children of parentId
if (a[i].parent === parentId) {
// recursively find children for each children of parentId
var ch = findFor(a[i].ID);
// if it has no children, skip adding the children prop
var o = Object.keys(ch).length === 0 ? {} : { children: ch };
z[a[i].ID] = Object.assign(o, a[i]);
}
}
return z;
}
// find all item whose parent is "0"
console.log(findFor("0"));
var a = [
{ ID: "1671", parent: "0", },
{ ID: "1223", parent: "0", },
{ ID: "1668", parent: "0", },
{ ID: "1688", parent: "0", },
{ ID: "1669", parent: "0", },
{ ID: "1681", parent: "1669", },
{ ID: "1680", parent: "1669", },
{ ID: "1670", parent: "1669", },
{ ID: "1682", parent: "1669", },
{ ID: "1433", parent: "1682", },
{ ID: "1684", parent: "1682", },
{ ID: "1672", parent: "1684", },
{ ID: "1685", parent: "1672", },
{ ID: "1686", parent: "1672", },
{ ID: "1683", parent: "0", },
{ ID: "1230", parent: "0", },
{ ID: "1667", parent: "0", },
{ ID: "1687", parent: "0", }
];
function findFor(parentId) {
var z = {};
for (var i = 0; i<a.length; i++){
if (a[i].parent === parentId) {
var ch = findFor(a[i].ID);
var o = Object.keys(ch).length === 0 ? {} : { children: ch };
z[a[i].ID] = Object.assign(o, a[i]);
}
}
return z;
}
console.log(findFor("0"));
这篇关于从平面数组创建嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!