gpt4 book ai didi

javascript - 按层次结构级别嵌套对象数组 JavaScript/TypeScript

转载 作者:行者123 更新时间:2023-12-01 02:11:03 25 4
gpt4 key购买 nike

我有一个类似于以下数据集:

dataSet = {rows: [
{"ID":0, "Value":10, "HIERARCHY_LEVEL":1},
{"ID":1, "Value":20, "HIERARCHY_LEVEL":2},
{"ID":2, "Value":55, "HIERARCHY_LEVEL":3},
{"ID":3, "Value":77, "HIERARCHY_LEVEL":2},
{"ID":4, "Value":29, "HIERARCHY_LEVEL":3},
{"ID":5, "Value":44, "HIERARCHY_LEVEL":4},
{"ID":6, "Value":34, "HIERARCHY_LEVEL":5},
{"ID":7, "Value":56, "HIERARCHY_LEVEL":4},
{"ID":8, "Value":23, "HIERARCHY_LEVEL":2},
{"ID":9, "Value":23, "HIERARCHY_LEVEL":3},
{"ID":10, "Value":23, "HIERARCHY_LEVEL":1},
{"ID":11, "Value":23, "HIERARCHY_LEVEL":2},
{"ID":12, "Value":23, "HIERARCHY_LEVEL":2},
{"ID":13, "Value":23, "HIERARCHY_LEVEL":3},
]}

我想按层次结构级别重新排列此数据集,因此每个级别都放入前一个对象的 children 属性数组中,如下所示:

NOTE: The dataSet is loaded in, as is (in-order). Also, it does not matter if all objects have the children property.

(我已尝试对其进行格式化以便于阅读)

newDataSet = [
{"ID":0, "Value":10, "HIERARCHY_LEVEL":1, "children":[
{"ID":1, "Value":20, "HIERARCHY_LEVEL":2, "children":[
{"ID":2, "Value":55, "HIERARCHY_LEVEL":3}
]},
{"ID":3, "Value":77, "HIERARCHY_LEVEL":2, "children":[
{"ID":4, "Value":29, "HIERARCHY_LEVEL":3, "children":[
{"ID":5, "Value":44, "HIERARCHY_LEVEL":4, "children":[
{"ID":6, "Value":34, "HIERARCHY_LEVEL":5}
]},
{"ID":7, "Value":56, "HIERARCHY_LEVEL":4}
]}
]},
{"ID":8, "Value":23, "HIERARCHY_LEVEL":2, "children":[
{"ID":9, "Value":23, "HIERARCHY_LEVEL":3}
]}
]},
{"ID":10, "Value":23, "HIERARCHY_LEVEL":1, "children":[
{"ID":11, "Value":23, "HIERARCHY_LEVEL":2},
{"ID":12, "Value":23, "HIERARCHY_LEVEL":2, "children":[
{"ID":13, "Value":23, "HIERARCHY_LEVEL":3}
]}
]}
]

编辑

我当前的代码

let newResults = [];
let topHierarchyLevel = 1;
let resultCounter = (newResults.length - 1); //Get the 2nd last element of newResults
let currentLevel = 2;
let loopCounter = 0;
for (loopCounter; loopCounter < dataSet.rows.length; loopCounter++) {
let row = dataSet.rows[loopCounter];
row["children"] = [];
if (row.HIERARCHY_LEVEL == topHierarchyLevel) {
newResults.push(row);
resultCounter++;
} else {
if (row.HIERARCHY_LEVEL == 2) {
newResults[resultCounter].children.push(row);
currentLevel++;
} else if (row.HIERARCHY_LEVEL > 2) {
newResults[resultCounter-1].children.children.push(row);
currentLevel++;
resultCounter++;
} else {
currentLevel--;
resultCounter--;
loopCounter--;
}
}
}

最佳答案

您可以使用级别属性HIERARCHY_LEVEL来指示辅助数组中的嵌套位置。

然后迭代数据并构建子数组(如有必要)。

function getTree(array) {
var levels = [{}];
array.forEach(function (o) {
levels.length = o.HIERARCHY_LEVEL;
levels[o.HIERARCHY_LEVEL - 1].children = levels[o.HIERARCHY_LEVEL - 1].children || [];
levels[o.HIERARCHY_LEVEL - 1].children.push(o);
levels[o.HIERARCHY_LEVEL] = o;
});
return levels[0].children;
}

var data = [{ ID: 0, Value: 10, HIERARCHY_LEVEL: 1 }, { ID: 1, Value: 20, HIERARCHY_LEVEL: 2 }, { ID: 2, Value: 55, HIERARCHY_LEVEL: 3 }, { ID: 3, Value: 77, HIERARCHY_LEVEL: 2 }, { ID: 4, Value: 29, HIERARCHY_LEVEL: 3 }, { ID: 5, Value: 44, HIERARCHY_LEVEL: 4 }, { ID: 6, Value: 34, HIERARCHY_LEVEL: 5 }, { ID: 7, Value: 56, HIERARCHY_LEVEL: 4 }, { ID: 8, Value: 23, HIERARCHY_LEVEL: 2 }, { ID: 9, Value: 23, HIERARCHY_LEVEL: 3 }, { ID: 10, Value: 23, HIERARCHY_LEVEL: 1 }, { ID: 11, Value: 23, HIERARCHY_LEVEL: 2 }, { ID: 12, Value: 23, HIERARCHY_LEVEL: 2 }, { ID: 13, Value: 23, HIERARCHY_LEVEL: 3 }];

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

关于javascript - 按层次结构级别嵌套对象数组 JavaScript/TypeScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49774256/

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