json 基于公共字段合并对象数组

7ajki6be  于 2023-02-14  发布在  其他
关注(0)|答案(3)|浏览(175)

我有一个对象,看起来如下所示:

[
    {
        "Net_Amount": 499,
        "Date": "2022-01-09T18:30:00.000Z",
        "Scheme_Name": "CUSTOMERWINBACKJCA01",
        "Month": "Jan"
    },
    {
        "Net_Amount": 902,
        "Date": "2022-01-09T18:30:00.000Z",
        "Scheme_Name": "CUSTOMERWINBACKJCA02",
        "Month": "Jan"
    },
    {
        "Net_Amount": 1860,
        "Date": "2022-10-01T18:30:00.000Z",
        "Scheme_Name": "CUSTOMERCONNECTJCA",
        "Month": "Oct"
    },
    {
        "Net_Amount": 1889,
        "Date": "2022-11-01T18:30:00.000Z",
        "Scheme_Name": "CUSTOMERCONNECTJCA",
        "Month": "Nov"
    }
]

现在,如果你仔细看,我有一个共同的字段月在对象中,我想合并的对象只基于这个共同的字段。我希望我的对象是如何格式化为:

[
    {
        "Month": "Jan",
        "varData":  [{
            "Net_Amount": 499,
            "Date": "2022-01-09T18:30:00.000Z",
            "Scheme_Name": "CUSTOMERWINBACKJCA01"
        },
        {
            "Net_Amount": 902,
            "Date": "2022-01-09T18:30:00.000Z",
            "Scheme_Name": "CUSTOMERWINBACKJCA02"
        }]
    },
    {
        "Month": "Oct",
        "varData":  [{
            "Net_Amount": 1860,
            "Date": "2022-10-01T18:30:00.000Z",
            "Scheme_Name": "CUSTOMERCONNECTJCA"
        }]
    },
    {
        "Month": "Nov",
        "varData":  [{
            "Net_Amount": 1889,
            "Date": "2022-11-01T18:30:00.000Z",
            "Scheme_Name": "CUSTOMERCONNECTJCA"
        }]
    }
]

我可以通过迭代数组并检查month是否相同,然后在varData中按下另一个键及其对象值来完成,但我想知道是否有任何快捷方式或内置函数可以用来实现我的目的。

ykejflvf

ykejflvf1#

我不认为有比迭代数组更好的内置解决方案。
但是如果您使用月份名称作为键,那么代码可能会非常简单(输出不完全相同,但结构非常相似)。

const result = {}
for (const entry of list) {
  if (!result[entry.Month]) {
     result[entry.Month] = []
  }
  result[entry.Month].push(entry)
}

参见jsfiddle
如果您需要的输出与问题中指定的完全相同,则可以使用以下代码:

let result = {}
for (const entry of list) {
  const month = entry.Month
  if (!result[month]) {
     result[month] = {
      "Month":   month,
      "varData": []
     }
  }
  delete entry.Month
  result[month].varData.push(entry)
}
result = Object.values(result)

参见jsfiddle

dfty9e19

dfty9e192#

const data = [{"Net_Amount":499,"Date":"2022-01-09T18:30:00.000Z","Scheme_Name":"CUSTOMERWINBACKJCA01","Month":"Jan"},{"Net_Amount":902,"Date":"2022-01-09T18:30:00.000Z","Scheme_Name":"CUSTOMERWINBACKJCA02","Month":"Jan"},{"Net_Amount":1860,"Date":"2022-10-01T18:30:00.000Z","Scheme_Name":"CUSTOMERCONNECTJCA","Month":"Oct"},{"Net_Amount":1889,"Date":"2022-11-01T18:30:00.000Z","Scheme_Name":"CUSTOMERCONNECTJCA","Month":"Nov"}]

console.log([...new Set(data.map(i=>i.Month))].map(Month=>
  ({Month, varData: data.filter(({Month:m})=>m===Month).map(({Month,...o})=>o)})))
krcsximq

krcsximq3#

const dataArr = [
    {
        Net_Amount: 499,
        Date: "2022-01-09T18:30:00.000Z",
        Scheme_Name: "CUSTOMERWINBACKJCA01",
        Month: "Jan",
    },
    {
        Net_Amount: 902,
        Date: "2022-01-09T18:30:00.000Z",
        Scheme_Name: "CUSTOMERWINBACKJCA02",
        Month: "Jan",
    },
    {
        Net_Amount: 1860,
        Date: "2022-10-01T18:30:00.000Z",
        Scheme_Name: "CUSTOMERCONNECTJCA",
        Month: "Oct",
    },
    {
        Net_Amount: 1889,
        Date: "2022-11-01T18:30:00.000Z",
        Scheme_Name: "CUSTOMERCONNECTJCA",
        Month: "Nov",
    },
];

const outputObj = dataArr.reduce((acc, crt) => {
    acc[crt.Month] ??= [];
    acc[crt.Month].push(crt);
    return acc;
}, {});

const outputArr = Object.values(outputObj).map((item) => ({ Month: item[0].Month, varData: item }));

console.log(outputArr);

相关问题