通过键减少数组对象并返回嵌套对象
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过键减少数组对象并返回嵌套对象相关的知识,希望对你有一定的参考价值。
我有一个对象数组。
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}
]
目标是首先根据Year和monthNumber将所有键值动态求和到一个数组中。
原始数组只是一个示例,除了'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],
}
}
答案
您可以采用动态方法,将year
和monthNumber
从对象中进行分组。
稍后从对象创建plain
数组。
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; }
另一答案
data = data.reduce(function(total, currentValue){
let year = currentValue.year;
let monthNumber = currentValue.monthNumber;
if(!total.rev[year]){
total.rev[year] = [];
total.exp[year] = [];
total.profit[year] = [];
}
if(!total.rev[year][monthNumber]){
total.rev[year][monthNumber] = 0;
total.exp[year][monthNumber] = 0;
total.profit[year][monthNumber] = 0;
}
if(typeof currentValue.rev == "number") total.rev[year][monthNumber] += currentValue.rev;
if(typeof currentValue.exp == "number") total.exp[year][monthNumber] += currentValue.exp;
if(typeof currentValue.profit == "number") total.profit[year][monthNumber] += currentValue.profit;
return total;
}, {
rev : {},
exp : {},
profit : {}
});
另一答案
您可以浏览所有值并根据年份进行添加
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}];
var _desired = {rev:{},exp:{},profit:{}}
_data.forEach(key=>{
if(!_desired.rev[key.year]){
_desired.rev[key.year] = [];
}
if(!_desired.exp[key.year]){
_desired.exp[key.year] = [];
}
if(!_desired.profit[key.year]){
_desired.profit[key.year] = [];
}
_desired.rev[key.year].push(key.rev);
_desired.exp[key.year].push(key.exp);
_desired.profit[key.year].push(key.profit);
})
console.log(_desired);
另一答案
尝试一下,我不知道它是否符合您的目的:
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: 1, rev: 32354, exp: 11184, profit: 21170},
{year: 2018, monthNumber: 1, rev: 30620, exp: 16974, EBITDA: 13646},
{year: 2018, monthNumber: 1, rev: 9050, exp: 12431, profit: -3381},
{year: 2019, monthNumber: 1, rev: 9050, exp: 12522, profit: -3472},
{year: 2019, monthNumber: 2, rev: 12265, exp: 12752, profit: -487},
{year: 2018, monthNumber: 2, rev: 9955, exp: 12424, profit: -2469},
{year: 2018, monthNumber: 3, rev: 13657, exp: 13424, profit: 233},
{year: 2019, monthNumber: 3, rev: 9050, exp: 12410, profit: -3360},
{year: 2019, monthNumber: 3, rev: 15045, exp: 13315, profit: 1730}
]
var result={};
function simplearray(obj){
//alert(obj.year);
if(typeof result[obj.year]== "undefined")
{
result[obj.year]={};
}
if(typeof result[obj.year][obj.monthNumber]== "undefined")
{
result[obj.year][obj.monthNumber]={};
}
if(typeof result[obj.year][obj.monthNumber]["rev"]== "undefined")
{
result[obj.year][obj.monthNumber]["rev"]=0;
result[obj.year][obj.monthNumber]["exp"]=0;
result[obj.year][obj.monthNumber]["profit"]=0;
}
result[obj.year][obj.monthNumber]["rev"]+=obj.rev;
result[obj.year][obj.monthNumber]["exp"]+=obj.exp;
result[obj.year][obj.monthNumber]["profit"]+=obj.profit;
}
for (a=0;a<data.length;a++)
{
simplearray(data[a]);
}
console.log(result);
console.log(result[2018][2].rev);
另一答案
第二种方式,如果monthNumber的索引正确:
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}
]
var result={};
function simplearray(obj){
if(typeof result["rev"]== "undefin以上是关于通过键减少数组对象并返回嵌套对象的主要内容,如果未能解决你的问题,请参考以下文章