通过键减少数组对象并返回嵌套对象

时间:2019-10-26 14:37:53

标签: javascript arrays object

我有一个对象数组。

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],
    }
}

6 个答案:

答案 0 :(得分:3)

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

稍后从对象创建一个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; }

答案 1 :(得分: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 : {}
});

答案 2 :(得分:0)

您可以浏览所有值并根据年份进行添加

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);

答案 3 :(得分:0)

尝试一下,我不知道它是否会符合您的目的:

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);

答案 4 :(得分:0)

第二种方式,如果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"]== "undefined")
  {
    result["rev"]={};
  }
  if(typeof result["rev"][obj.year]== "undefined")
  {
    result["rev"][obj.year]=[];
  }      

  if(typeof result["rev"][obj.year][obj.monthNumber]== "undefined")
  {
    result["rev"][obj.year][obj.monthNumber]=0;
  }      
  result["rev"][obj.year][obj.monthNumber]+=obj.rev;

  if(typeof result["exp"]== "undefined")
  {
    result["exp"]={};
  }
  if(typeof result["exp"][obj.year]== "undefined")
  {
    result["exp"][obj.year]=[];
  }      

  if(typeof result["exp"][obj.year][obj.monthNumber]== "undefined")
  {
    result["exp"][obj.year][obj.monthNumber]=0;
  }      
  result["exp"][obj.year][obj.monthNumber]+=obj.exp;
  
  if(typeof result["profit"]== "undefined")
  {
    result["profit"]={};
  }
  if(typeof result["profit"][obj.year]== "undefined")
  {
    result["profit"][obj.year]=[];
  }      

  if(typeof result["profit"][obj.year][obj.monthNumber]== "undefined")
  {
    result["profit"][obj.year][obj.monthNumber]=0;
  }      
  result["profit"][obj.year][obj.monthNumber]+=obj.profit;
}

for (a=0;a<data.length;a++)
{
  simplearray(data[a]);
}   


console.log(result);
console.log(result["profit"][2018][0]);

答案 5 :(得分:-3)

您应该签出enter image description here(可独立下载)并使用分组功能。它对给定属性的数组进行分组。由于您未发布任何代码,因此我相信您正在寻找lib。