gpt4 book ai didi

javascript - 从平面数组创建嵌套对象

转载 作者:行者123 更新时间:2023-11-28 14:35:17 25 4
gpt4 key购买 nike

我目前有一个对象数组,我正在尝试使用 ID 将其 reshape 为嵌套对象。作为对象键,并以 ID 为目标与 parent id 如果不是 "0" 。我尝试了几种方法,但我很挣扎! 对我来说的主要障碍是任何超过一两层深度的东西。理想情况下,我需要它是动态的,这样它就可以处理任何潜在的深度,尽管在野外我怀疑它会超过 3 层。

这是传入的对象数组:

[

{
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()和其他类型的递归但无济于事,所以我有兴趣看看是否有解决方案。

谢谢

最佳答案

您可以像这样使用递归函数:

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

关于javascript - 从平面数组创建嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49974999/

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