gpt4 book ai didi

javascript - 将对象数组(带有字符串数组元素)转换为嵌套树

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

一段时间以来,我一直在尝试解决数组和对象的问题 - 但我仍然找不到正确的解决方案。

这是我的起始位置:

list = [
{
lastItemData: {data:"root"},
path: ["Root"]
},
{
lastItemData: {data:"level2_Summer"},
path: ["Root", "Level2_Summer"]
},
{
lastItemData: {data:"Level3_Monday"},
path: ["Root", "Level2_Winter", "Level3_Monday"]
},
{
lastItemData: {data:"Level4_Morning"},
path: ["Root", "Level2_Spring", "Level3_Tuesday", "Level4_Morning"]
},
{
lastItemData: {data:"Level3_Sunday"},
path: ["Root", "Level2_Autumn", "Level3_Sunday"]
}]

这就是我需要的:

result = [
{
text: "Root",
lastItemData: {data:"root"},
Items:[
{
text:"Level2_Summer",
lastItemData: {data:"level2_Summer"},
Items: []
},
{
text:"Level2_Winter",
Items:[
{
text: "Level3_Monday",
lastItemData: {data:"Level3_Monday"},
Items: []
}
]
},
{
text:"Level2_Spring",
Items:[
{
text: "Level3_Tuesday"
Items: [
{
text:"Level4_Morning"
Items:[],
lastItemData: {data:"Level4_Morning"},
}
]
}
]
},
{
text:"Level2_Autumn",
Items:[
{
text: "Level3_Sunday"
}
]
},
]
}]

我尝试过这样的事情(代码基于我删除的帖子中的用户 jonas 回答)

const property = list.reduce((previous, current, index) => {


let acc = {}; //the accumulator that will go deep into the object
const result = acc; //our result will be the top level object

debugger;

//Now we iterate over our array
for (var i = 0; i < current.stringPath.length; i++) {
debugger;
//add the string part
acc["level" + (i + 1)] = current.stringPath[i];
//Add the children array
acc["Items"] = [{}];
//And go into the object
acc = acc["Items"][0];
}

console.log(result)

previous.push(result);

return previous;

}, []);

console.log("property", property);

但不幸的是,结果并不符合所需的结构。谁能帮帮我?

最佳答案

这是一个看起来要复杂得多的问题,因为您必须构建一个具有给定名称(text 属性)的嵌套结构,并且您需要检查该名称是否存在。如果没有创建一个新对象并将其推送到父数组。

此解决方案具有一个哈希表,其中收集和维护每个级别的所有嵌套哈希。结果是一个包含所需树的数组。

它的工作原理如下:

首先查看哈希表的根并检查路径值是否存在。如果不是,则使用该信息创建一个新节点并将其推送到哈希收集器,用 _ 表示。然后返回该节点并继续迭代给定路径。

在路径的 和 处使用 lastItemData 作为所需对象。

var list = [{ lastItemData: { data: "root" }, path: ["Root"] }, { lastItemData: { data: "level2_Summer" }, path: ["Root", "Level2_Summer"] }, { lastItemData: { data: "Level3_Monday" }, path: ["Root", "Level2_Winter", "Level3_Monday"] }, { lastItemData: { data: "Level4_Morning" }, path: ["Root", "Level2_Spring", "Level3_Tuesday", "Level4_Morning"] }, { lastItemData: { data: "Level3_Sunday" }, path: ["Root", "Level2_Autumn", "Level3_Sunday"] }],
tree = function (array) {
var result = [],
hash = { _: { Items: result } };

array.forEach(function (object) {
object.path.reduce(function (o, p) {
if (!o[p]) {
o[p] = { _: { text: p, Items: [] } };
o._.Items.push(o[p]._);
}
return o[p];
}, hash)._.lastItemData = object.lastItemData;
});
return result;
}(list);

console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 将对象数组(带有字符串数组元素)转换为嵌套树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48057230/

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