我有以下JSON响应,其中包含由几个对象(数据中心,环境)组成的多个对象:
"deployments": [
{
"datacenter": {
"title": "euw1",
"name": "foodatacenter",
"revision": "0",
"state": "Active"
},
"environment": {
"clusterId": "AAA",
"name": "foocluster",
"revision": "0",
"state": "Active",
}
},
{
"datacenter": {
"title": "apc1",
"name": "foodatacenter",
"revision": "0",
"state": "Active"
},
"environment": {
"clusterId": "BBB",
"name": "foocluster",
"revision": "0",
"state": "Active"
}
},
{
"datacenter": {
"title": "euw1",
"name": "foodatacenter",
"revision": "0",
"state": "Active"
},
"environment": {
"clusterId": "BBB",
"name": "foocluster",
"revision": "0",
"state": "Active"
}
},
{
"datacenter": {
"title": "use1",
"name": "foodatacenter",
"revision": "0",
"state": "Active"
},
"environment": {
"clusterId": "AAA",
"name": "foocluster",
"revision": "0",
"state": "Active"
}
},
{
"datacenter": {
"title": "use2",
"name": "foodatacenter",
"revision": "0",
"state": "Active"
},
"environment": {
"clusterId": "AAA",
"name": "foocluster",
"revision": "0",
"state": "Active"
}
},
]
使用javascript按环境分组的最佳方法是什么?所需的JSON结果如下:
"deployments": [
{
"clusterId": "AAA",
"name": "foocluster",
"revision": "0",
"state": "Active",
"datacenters":[
{
"title": "euw1",
"name": "foodatacenter",
"revision": "0",
"state": "Active"
},
{
"title": "use1",
"name": "foodatacenter",
"revision": "0",
"state": "Active"
},
{
"title": "use2",
"name": "foodatacenter",
"revision": "0",
"state": "Active"
},
]
},
{
"clusterId": "BBB",
"name": "foocluster",
"revision": "0",
"state": "Active",
"datacenters":[
{
"title": "euw1",
"name": "foodatacenter",
"revision": "0",
"state": "Active"
},
{
"title": "apc1",
"name": "foodatacenter",
"revision": "0",
"state": "Active"
}
]
}
]
非常感谢您的帮助。 非常感谢你!
答案 0 :(得分:2)
除了在数组上循环并按datacenter
收集environment
个对象外,我没有看到任何其他选项。在伪代码中,这将是:
datacenter
添加到数组中以获取密钥environment
environment
和datacenter
个对象的字段。答案 1 :(得分:1)
您可以使用array#reduce
根据对象中的environment
clusterId
对数据进行分组,并添加与类似datacenter
对应的所有clusterId
,然后提取来自对象的值。
const data = {"deployments": [ { "datacenter": { "title": "euw1", "name": "foodatacenter", "revision": "0", "state": "Active" }, "environment": { "clusterId": "AAA", "name": "foocluster", "revision": "0", "state": "Active", } }, { "datacenter": { "title": "apc1", "name":"foodatacenter", "revision": "0", "state": "Active" }, "environment": { "clusterId": "BBB", "name": "foocluster", "revision": "0", "state": "Active" } }, { "datacenter": { "title": "euw1", "name": "foodatacenter", "revision": "0", "state": "Active" },"environment": { "clusterId": "BBB", "name": "foocluster", "revision": "0", "state": "Active" } }, { "datacenter": { "title": "use1", "name": "foodatacenter", "revision": "0", "state": "Active" }, "environment": { "clusterId": "AAA", "name": "foocluster","revision": "0", "state": "Active" } }, { "datacenter": { "title": "use2", "name": "foodatacenter", "revision": "0", "state": "Active" }, "environment": { "clusterId": "AAA", "name": "foocluster", "revision": "0", "state": "Active" } }]},
result = Object.values(data.deployments.reduce((r,{datacenter, environment}) => {
const key = environment.clusterId;
r[key] = r[key] || {...environment, datacenters : []};
r[key].datacenters.push({...datacenter});
return r;
},{}));
console.log(result);

"use strict";
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var data = { "deployments": [{ "datacenter": { "title": "euw1", "name": "foodatacenter", "revision": "0", "state": "Active" }, "environment": { "clusterId": "AAA", "name": "foocluster", "revision": "0", "state": "Active" } }, { "datacenter": { "title": "apc1", "name": "foodatacenter", "revision": "0", "state": "Active" }, "environment": { "clusterId": "BBB", "name": "foocluster", "revision": "0", "state": "Active" } }, { "datacenter": { "title": "euw1", "name": "foodatacenter", "revision": "0", "state": "Active" }, "environment": { "clusterId": "BBB", "name": "foocluster", "revision": "0", "state": "Active" } }, { "datacenter": { "title": "use1", "name": "foodatacenter", "revision": "0", "state": "Active" }, "environment": { "clusterId": "AAA", "name": "foocluster", "revision": "0", "state": "Active" } }, { "datacenter": { "title": "use2", "name": "foodatacenter", "revision": "0", "state": "Active" }, "environment": { "clusterId": "AAA", "name": "foocluster", "revision": "0", "state": "Active" } }] },
result = Object.values(data.deployments.reduce(function (r, o) {
var datacenter = o.datacenter,
environment = o.environment;
var key = environment.clusterId;
r[key] = r[key] || _extends({}, environment, { datacenters: [] });
r[key].datacenters.push(_extends({}, datacenter));
return r;
}, {}));
console.log(result);
&#13;