如何分组和合并由几个对象组成的JSON对象?

时间:2018-04-30 12:27:44

标签: javascript json

我有以下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"
                               }
                           ]
                    }

    ]

非常感谢您的帮助。 非常感谢你!

2 个答案:

答案 0 :(得分:2)

除了在数组上循环并按datacenter收集environment个对象外,我没有看到任何其他选项。在伪代码中,这将是:

  1. 表示数组中的每个对象
    1. key = JSON.stringify(o.environment);
    2. 在字典中找到密钥。
    3. 如果找不到密钥,请添加密钥,并将数组添加为值
    4. datacenter添加到数组中以获取密钥
  2. 创建结果数组
  3. 为词典中的每个项目
    1. 从密钥
    2. 创建environment
    3. 在结果中创建一个项目,其中包含值中environmentdatacenter个对象的字段。

答案 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;
&#13;
&#13;