【发布时间】:2019-08-20 17:57:09
【问题描述】:
解释:有一个输入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);
任何直接的帮助都将非常重要。谢谢
【问题讨论】:
-
发布一些您使用的代码,因为我不是向导,或者可能编辑您的问题,因为不清楚您有什么,您想要实现什么,以及如何实现
-
我已经放了我尝试过的代码,并且我想要从输入 JSON 中输出 JSON 结构。
-
哦不好意思没看到,以为最后一个只有json
标签: javascript arrays json ecmascript-6