代码之家  ›  专栏  ›  技术社区  ›  Mahesh G

如何合并具有相同名称的JSON对象

  •  0
  • Mahesh G  · 技术社区  · 7 年前

    最初我有Json数组,它是使用 underscore groupBy 函数,我已经分组了,每个都分组了,但我想再次合并所有具有相同对象名称的数组,下面是我想要实现的方式,有人能帮我吗。

    versionId 在JSON数组中,现在我想合并所有具有相同

    有人能帮上忙吗 强调 Lodash 框架

    输入JSON

    [
      {
        "21289": [
          {
            "id": 255463,
            "orderId": 226433,
            "status": 1,
            "executedOn": "25/Sep/17",
            "executedBy": "Person A",
            "executedByDisplay": "Person A",
            "cycleId": 4042,
            "cycleName": "Cycle A",
            "versionId": 21289,
            "versionName": "Version A",
            "issueKey": "A"
          },
          {
            "id": 255433,
            "orderId": 226403,
            "status": 1,
            "executedOn": "25/Sep/17",
            "executedBy": "Person B",
            "executedByDisplay": "Person B",
            "cycleId": 4041,
            "cycleName": "Cycle B",
            "versionId": 21289,
            "versionName": "Version A",
            "issueKey": "B"
          },
          {
            "id": 255432,
            "orderId": 226402,
            "status": 1,
            "executedOn": "25/Sep/17",
            "executedBy": "Person B",
            "executedByDisplay": "Person B",
            "cycleId": 4041,
            "cycleName": "Cycle B",
            "versionId": 21289,
            "versionName": "Version A",
            "issueKey": "C"
          }
        ],
        "21291": [
          {
            "id": 252067,
            "orderId": 223258,
            "status": 1,
            "executedOn": "12/Sep/17",
            "executedBy": "Person C",
            "executedByDisplay": "Person C",
            "cycleId": 3985,
            "cycleName": "Cycle C",
            "versionId": 21291,
            "versionName": "Version B",
            "issueKey": "D"
          },
          {
            "id": 252014,
            "orderId": 223205,
            "status": 1,
            "executedOn": "12/Sep/17",
            "executedBy": "Person C",
            "executedByDisplay": "Person C",
            "cycleId": 3985,
            "cycleName": "Cycle C",
            "versionId": 21291,
            "versionName": "Version B",
            "issueKey": "E"
          },
          {
            "id": 252012,
            "orderId": 223203,
            "status": 1,
            "executedOn": "11/Sep/17",
            "executedBy": "Person D",
            "executedByDisplay": "Person D",
            "cycleId": 3985,
            "cycleName": "Cycle C",
            "versionId": 21291,
            "versionName": "Version B",
            "issueKey": "F"
          }
        ]
      },
      {
        "21291": [
          {
            "id": 251449,
            "orderId": 222640,
            "status": 1,
            "executedOn": "31/Aug/17",
            "executedBy": "Person E",
            "executedByDisplay": "Person E",
            "cycleId": 3978,
            "cycleName": "Cycle D",
            "versionId": 21291,
            "versionName": "Version B",
            "issueKey": "G"
          },
          {
            "id": 251299,
            "orderId": 222490,
            "status": 1,
            "executedOn": "1/Sep/17",
            "executedBy": "Person F",
            "executedByDisplay": "Person F",
            "cycleId": 3977,
            "cycleName": "Cycle E",
            "versionId": 21291,
            "versionName": "Version B",
            "issueKey": "H"
          }
        ],
        "31457": [
          {
            "id": 250969,
            "orderId": 222160,
            "status": 1,
            "executedOn": "29/Aug/17",
            "executedBy": "Person G",
            "executedByDisplay": "Person G",
            "cycleId": 3973,
            "cycleName": "Cycle F",
            "versionId": 31457,
            "versionName": "Version C",
            "issueKey": "I"
          },
          {
            "id": 250871,
            "orderId": 222062,
            "status": 1,
            "executedOn": "31/Aug/17",
            "executedBy": "Person D",
            "executedByDisplay": "Person D",
            "cycleId": 3973,
            "cycleName": "Cycle F",
            "versionId": 31457,
            "versionName": "Version C",
            "issueKey": "J"
          },
          {
            "id": 250869,
            "orderId": 222060,
            "status": 1,
            "executedOn": "4/Sep/17",
            "executedBy": "Person H",
            "executedByDisplay": "Person H",
            "cycleId": 3973,
            "cycleName": "Cycle F",
            "versionId": 31457,
            "versionName": "Version C",
            "issueKey": "K"
          }
        ]
      }
    ]
    

    根据需要输出JSON

    [
      {
        "21289": [
          {
            "id": 255463,
            "orderId": 226433,
            "status": 1,
            "executedOn": "25/Sep/17",
            "executedBy": "Person A",
            "executedByDisplay": "Person A",
            "cycleId": 4042,
            "cycleName": "Cycle A",
            "versionId": 21289,
            "versionName": "Version A",
            "issueKey": "A"
          },
          {
            "id": 255433,
            "orderId": 226403,
            "status": 1,
            "executedOn": "25/Sep/17",
            "executedBy": "Person B",
            "executedByDisplay": "Person B",
            "cycleId": 4041,
            "cycleName": "Cycle B",
            "versionId": 21289,
            "versionName": "Version A",
            "issueKey": "B"
          },
          {
            "id": 255432,
            "orderId": 226402,
            "status": 1,
            "executedOn": "25/Sep/17",
            "executedBy": "Person B",
            "executedByDisplay": "Person B",
            "cycleId": 4041,
            "cycleName": "Cycle B",
            "versionId": 21289,
            "versionName": "Version A",
            "issueKey": "C"
          }
        ],
        "21291": [
          {
            "id": 252067,
            "orderId": 223258,
            "status": 1,
            "executedOn": "12/Sep/17",
            "executedBy": "Person C",
            "executedByDisplay": "Person C",
            "cycleId": 3985,
            "cycleName": "Cycle C",
            "versionId": 21291,
            "versionName": "Version B",
            "issueKey": "D"
          },
          {
            "id": 252014,
            "orderId": 223205,
            "status": 1,
            "executedOn": "12/Sep/17",
            "executedBy": "Person C",
            "executedByDisplay": "Person C",
            "cycleId": 3985,
            "cycleName": "Cycle C",
            "versionId": 21291,
            "versionName": "Version B",
            "issueKey": "E"
          },
          {
            "id": 252012,
            "orderId": 223203,
            "status": 1,
            "executedOn": "11/Sep/17",
            "executedBy": "Person D",
            "executedByDisplay": "Person D",
            "cycleId": 3985,
            "cycleName": "Cycle C",
            "versionId": 21291,
            "versionName": "Version B",
            "issueKey": "F"
          },
          {
            "id": 251449,
            "orderId": 222640,
            "status": 1,
            "executedOn": "31/Aug/17",
            "executedBy": "Person E",
            "executedByDisplay": "Person E",
            "cycleId": 3978,
            "cycleName": "Cycle D",
            "versionId": 21291,
            "versionName": "Version B",
            "issueKey": "G"
          },
          {
            "id": 251299,
            "orderId": 222490,
            "status": 1,
            "executedOn": "1/Sep/17",
            "executedBy": "Person F",
            "executedByDisplay": "Person F",
            "cycleId": 3977,
            "cycleName": "Cycle E",
            "versionId": 21291,
            "versionName": "Version B",
            "issueKey": "H"
          }
        ],
        "31457": [
          {
            "id": 250969,
            "orderId": 222160,
            "status": 1,
            "executedOn": "29/Aug/17",
            "executedBy": "Person G",
            "executedByDisplay": "Person G",
            "cycleId": 3973,
            "cycleName": "Cycle F",
            "versionId": 31457,
            "versionName": "Version C",
            "issueKey": "I"
          },
          {
            "id": 250871,
            "orderId": 222062,
            "status": 1,
            "executedOn": "31/Aug/17",
            "executedBy": "Person D",
            "executedByDisplay": "Person D",
            "cycleId": 3973,
            "cycleName": "Cycle F",
            "versionId": 31457,
            "versionName": "Version C",
            "issueKey": "J"
          },
          {
            "id": 250869,
            "orderId": 222060,
            "status": 1,
            "executedOn": "4/Sep/17",
            "executedBy": "Person H",
            "executedByDisplay": "Person H",
            "cycleId": 3973,
            "cycleName": "Cycle F",
            "versionId": 31457,
            "versionName": "Version C",
            "issueKey": "K"
          }
        ]
      }
    ]
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   bigless    7 年前

    如果您知道结构将完全像这样,那么这里有一个非常简单的解决方案:

    var a = [
      {
        "21289": [
          {
            "id": 255463,
            "orderId": 226433,
            "status": 1,
            "executedOn": "25/Sep/17",
            "executedBy": "Person A",
            "executedByDisplay": "Person A",
            "cycleId": 4042,
            "cycleName": "Cycle A",
            "versionId": 21289,
            "versionName": "Version A",
            "issueKey": "A"
          },
          {
            "id": 255433,
            "orderId": 226403,
            "status": 1,
            "executedOn": "25/Sep/17",
            "executedBy": "Person B",
            "executedByDisplay": "Person B",
            "cycleId": 4041,
            "cycleName": "Cycle B",
            "versionId": 21289,
            "versionName": "Version A",
            "issueKey": "B"
          },
          {
            "id": 255432,
            "orderId": 226402,
            "status": 1,
            "executedOn": "25/Sep/17",
            "executedBy": "Person B",
            "executedByDisplay": "Person B",
            "cycleId": 4041,
            "cycleName": "Cycle B",
            "versionId": 21289,
            "versionName": "Version A",
            "issueKey": "C"
          }
        ],
        "21291": [
          {
            "id": 252067,
            "orderId": 223258,
            "status": 1,
            "executedOn": "12/Sep/17",
            "executedBy": "Person C",
            "executedByDisplay": "Person C",
            "cycleId": 3985,
            "cycleName": "Cycle C",
            "versionId": 21291,
            "versionName": "Version B",
            "issueKey": "D"
          },
          {
            "id": 252014,
            "orderId": 223205,
            "status": 1,
            "executedOn": "12/Sep/17",
            "executedBy": "Person C",
            "executedByDisplay": "Person C",
            "cycleId": 3985,
            "cycleName": "Cycle C",
            "versionId": 21291,
            "versionName": "Version B",
            "issueKey": "E"
          },
          {
            "id": 252012,
            "orderId": 223203,
            "status": 1,
            "executedOn": "11/Sep/17",
            "executedBy": "Person D",
            "executedByDisplay": "Person D",
            "cycleId": 3985,
            "cycleName": "Cycle C",
            "versionId": 21291,
            "versionName": "Version B",
            "issueKey": "F"
          }
        ]
      },
      {
        "21291": [
          {
            "id": 251449,
            "orderId": 222640,
            "status": 1,
            "executedOn": "31/Aug/17",
            "executedBy": "Person E",
            "executedByDisplay": "Person E",
            "cycleId": 3978,
            "cycleName": "Cycle D",
            "versionId": 21291,
            "versionName": "Version B",
            "issueKey": "G"
          },
          {
            "id": 251299,
            "orderId": 222490,
            "status": 1,
            "executedOn": "1/Sep/17",
            "executedBy": "Person F",
            "executedByDisplay": "Person F",
            "cycleId": 3977,
            "cycleName": "Cycle E",
            "versionId": 21291,
            "versionName": "Version B",
            "issueKey": "H"
          }
        ],
        "31457": [
          {
            "id": 250969,
            "orderId": 222160,
            "status": 1,
            "executedOn": "29/Aug/17",
            "executedBy": "Person G",
            "executedByDisplay": "Person G",
            "cycleId": 3973,
            "cycleName": "Cycle F",
            "versionId": 31457,
            "versionName": "Version C",
            "issueKey": "I"
          },
          {
            "id": 250871,
            "orderId": 222062,
            "status": 1,
            "executedOn": "31/Aug/17",
            "executedBy": "Person D",
            "executedByDisplay": "Person D",
            "cycleId": 3973,
            "cycleName": "Cycle F",
            "versionId": 31457,
            "versionName": "Version C",
            "issueKey": "J"
          },
          {
            "id": 250869,
            "orderId": 222060,
            "status": 1,
            "executedOn": "4/Sep/17",
            "executedBy": "Person H",
            "executedByDisplay": "Person H",
            "cycleId": 3973,
            "cycleName": "Cycle F",
            "versionId": 31457,
            "versionName": "Version C",
            "issueKey": "K"
          }
        ]
      }
    ];
    
    function filterKeys(result, obj) {
      for (var key in obj) {
      	result[key] = (key in result? result[key].concat(obj[key]) : obj[key]);
      }
      return result;
    }
    console.log(a.reduce(filterKeys, {}));