从JSON对象构建树结构

时间:2019-03-29 18:17:25

标签: javascript arrays json ecmascript-6

说明::按照以下格式输入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);

任何立即帮助将非常可贵。谢谢

2 个答案:

答案 0 :(得分:4)

一个可能的解决方案是使用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;}

答案 1 :(得分:3)

我创建了一个具有不同测试场景的对象,遍历输入以填充测试场景,然后根据您的格式化请求将输出转换回数组。

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"
}];

function convertJson(inp) {
    let out = {};
    
    inp.forEach(function(o) {
       if (!out[o.TestScenario]) {
         out[o.TestScenario] = {};
       }
       
       if (!out[o.TestScenario][o.Application.trim()]) {
         out[o.TestScenario][o.Application.trim()] = [];
       }
       
       out[o.TestScenario][o.Application.trim()].push(o.Market.trim());
       
   });
   
  let outArray = Object.keys(out).map(function(k) {
    let d = {};
    d[k] = out[k];
    
    return d;
  });
  
  return outArray;
}

console.log(convertJson(inputJSON));