gpt4 book ai didi

javascript - 从 JSON 对象构建树结构

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

有一个符合以下格式的输入 JSON。

var inputJSON = [{
"TestScenario": "test1",
"Application": "application1",
"Market": "M1"
}, {
"TestScenario": "test1",
"Application": "application1",
"Market": "M2"
}, {
"TestScenario": "test1",
"Application": "application1",
"Market": "M3"
}, {
"TestScenario": "test1",
"Application": "application1",
"Market": "M4"
}, {
"TestScenario": "test2",
"Application": "application2",
"Market": "M5"
}, {
"TestScenario": "test2",
"Application": "application3",
"Market": "M5"
}];

它应该被构造成下面格式的树。

var outputJSON = [{
"test1": {
"application1": ["M1", "M2", "M3", "M4"]
}
}, {
"test2": {
"application2": "M5",
"application3": "M5"
}
}];

到目前为止我尝试了什么:

我能够使用一种 TestScenario 实现树格式,但会出现多个代码中断。

同类 TestScenario 的工作代码:

var defaultArrays = [{
"TestScenario": "test1",
"Application": "application1",
"Market": "M1"
}, {
"TestScenario": "test1",
"Application": "application1",
"Market": "M2"
}, {
"TestScenario": "test1",
"Application": "application1",
"Market": "M3"
}, {
"TestScenario": "test1",
"Application": "application1",
"Market": "M4"
}];

var testScenario = [];

for (const data of defaultArrays) {
if(testScenario.indexOf(data.TestScenario) === -1) {
testScenario.push(data.TestScenario);
}
}

var marketArray = [];
var shouldLookLikeThis = [];
var obj = {};
for (const b of defaultArrays) {
for (const c of testScenario) {
if (b.TestScenario === c) {
obj[c] = {};
obj[c][b.Application] = [];
}
if (shouldLookLikeThis.indexOf(obj) === -1) {
shouldLookLikeThis.push(obj);
}
}

for (const c of shouldLookLikeThis) {
var arr1 = Object.keys(c);
for (const d of arr1) {
if (b.TestScenario === d) {
var arr2 = Object.keys(c[d]);
for (const e of arr2) {
if(b.Application === e) {
marketArray.push(b.Market);
c[d][e] = marketArray;
}
}
}
}
}
}

console.log('shouldLookLikeThis', shouldLookLikeThis);

不使用多个 TestScenario:

var defaultArrays = [{
"TestScenario": "test1",
"Application": "application1",
"Market": "M1"
}, {
"TestScenario": "test1",
"Application": "application1",
"Market": "M2"
}, {
"TestScenario": "test1",
"Application": "application1",
"Market": "M3"
}, {
"TestScenario": "test1",
"Application": "application1",
"Market": "M4"
}, {
"TestScenario": "test2",
"Application": "application2",
"Market": "M5"
}, {
"TestScenario": "test2",
"Application": "application3",
"Market": "M5"
}];
var testScenario = [];

for (const data of defaultArrays) {
if(testScenario.indexOf(data.TestScenario) === -1) {
testScenario.push(data.TestScenario);
}
}

var marketArray = [];
var shouldLookLikeThis = [];
var obj = {};
for (const b of defaultArrays) {
for (const c of testScenario) {
if (b.TestScenario === c) {
obj[c] = {};
obj[c][b.Application] = [];
}
if (shouldLookLikeThis.indexOf(obj) === -1) {
shouldLookLikeThis.push(obj);
}
}

for (const c of shouldLookLikeThis) {
var arr1 = Object.keys(c);
for (const d of arr1) {
if (b.TestScenario === d) {
var arr2 = Object.keys(c[d]);
for (const e of arr2) {
if(b.Application === e) {
marketArray.push(b.Market);
c[d][e] = marketArray;
}
}
}
}
}
}

console.log('shouldLookLikeThis', shouldLookLikeThis);

最佳答案

一种可能的解决方案是使用 Array.reduce()首先进行分组程序。在此之后,您可以 Array.map() Object.entries()以前的结果得到你想要的结构。

示例:

var inputJSON = [
{"TestScenario": "test1", "Application": "application1", "Market": "M1"},
{"TestScenario": "test1", "Application": "application1", "Market": "M2"},
{"TestScenario": "test1", "Application": "application1", "Market": "M3"},
{"TestScenario": "test1", "Application": "application1", "Market": "M4"},
{"TestScenario": "test2", "Application": "application2", "Market": "M5"},
{"TestScenario": "test2", "Application": "application3", "Market": "M5"}
];

let res = inputJSON.reduce((acc, {TestScenario, Application, Market}) =>
{
acc[TestScenario] = acc[TestScenario] || {};
acc[TestScenario][Application] = acc[TestScenario][Application] || [];
acc[TestScenario][Application].push(Market);
return acc;
}, {})

res = Object.entries(res).map(([key, val]) => ({[key]: val}));

console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

关于javascript - 从 JSON 对象构建树结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55423416/

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