gpt4 book ai didi

javascript - 按键减少数组对象并返回嵌套对象

转载 作者:行者123 更新时间:2023-11-28 03:26:45 24 4
gpt4 key购买 nike

我有一个对象数组。

let data = [
{year: 2018, monthNumber: 0, rev: 40984, exp: 15525, profit: 25459}
{year: 2018, monthNumber: 0, rev: 162457, exp: 53608, profit: 108849}
{year: 2019, monthNumber: 0, rev: 8935, exp: 12514, profit: -3579}
{year: 2019, monthNumber: 0, rev: 32354, exp: 11184, profit: 21170}
{year: 2018, monthNumber: 1, rev: 30620, exp: 16974, profit: 13646}
{year: 2018, monthNumber: 1, rev: 9050, exp: 12431, profit: -3381}
{year: 2019, monthNumber: 1, rev: 9050, exp: 12522, profit: -3472}
{year: 2019, monthNumber: 1, rev: 12265, exp: 12752, profit: -487}
{year: 2018, monthNumber: 2, rev: 9955, exp: 12424, profit: -2469}
{year: 2018, monthNumber: 2, rev: 13657, exp: 13424, profit: 233}
{year: 2019, monthNumber: 2, rev: 9050, exp: 12410, profit: -3360}
{year: 2019, monthNumber: 2, rev: 15045, exp: 13315, profit: 1730}
]

目标是首先根据年份和月份将所有键值动态求和到一个数组中。

原始数组只是一个示例,除了“exp”、“profit”等之外,还有多个附加键。我想让它动态化并避免提及每个键。

[
{year: 2018, monthNumber: 0, rev: 203441, exp: 69133, profit: 134308}
{year: 2019, monthNumber: 0, rev: 41289, exp: 23698, profit: 17591}
{year: 2018, monthNumber: 1, rev: 39670, exp: 29405, profit: 10265}
{year: 2019, monthNumber: 1, rev: 21315, exp: 25274, profit: -3959}
{year: 2018, monthNumber: 2, rev: 23612, exp: 25848, profit: -2236}
{year: 2019, monthNumber: 2, rev: 24095, exp: 25725, profit: -1630}
]

然后获得以下所需的输出:

{ 
rev : {
2019 : [203441, 39670, 23612],
2018 : [41289, 21315, 24095],
},
exp : {
2019 : [69133, 29405, 25848],
2018 : [23698, 25274, 25725],
},
profit: {
2019 : [134308, 10265, -2236],
2018 : [17591, -3959, -1630],
}
}

最佳答案

您可以采用动态方法,从对象中取出 yearmonthNumber 进行分组。

稍后从该对象创建一个普通数组。

var data = [{ year: 2018, monthNumber: 0, rev: 40984, exp: 15525, profit: 25459 }, { year: 2018, monthNumber: 0, rev: 162457, exp: 53608, profit: 108849 }, { year: 2019, monthNumber: 0, rev: 8935, exp: 12514, profit: -3579 }, { year: 2019, monthNumber: 0, rev: 32354, exp: 11184, profit: 21170 }, { year: 2018, monthNumber: 1, rev: 30620, exp: 16974, profit: 13646 }, { year: 2018, monthNumber: 1, rev: 9050, exp: 12431, profit: -3381 }, { year: 2019, monthNumber: 1, rev: 9050, exp: 12522, profit: -3472 }, { year: 2019, monthNumber: 1, rev: 12265, exp: 12752, profit: -487 }, { year: 2018, monthNumber: 2, rev: 9955, exp: 12424, profit: -2469 }, { year: 2018, monthNumber: 2, rev: 13657, exp: 13424, profit: 233 }, { year: 2019, monthNumber: 2, rev: 9050, exp: 12410, profit: -3360 }, { year: 2019, monthNumber: 2, rev: 15045, exp: 13315, profit: 1730 }],
result = data.reduce((r, { year, monthNumber, ...o }) => {
Object.entries(o).forEach(([k, v]) => {
r[k] = r[k] || {};
r[k][year] = r[k][year] || [];
r[k][year][monthNumber] = (r[k][year][monthNumber] || 0) + v;
});
return r;
}, {}),
plain = Object.entries(result).reduce((r, [k, years]) => {
Object.entries(years).forEach(([year, array]) => array.forEach((v, monthNumber) => {
var temp = r.find(q => q.year === year && q.monthNumber === monthNumber);
if (!temp) r.push(temp = { year, monthNumber });
temp[k] = v;
}));
return r;
}, []);

console.log(result);
console.log(plain);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 按键减少数组对象并返回嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58571752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com