我有一组对象需要根据键后缀求和

I've an array of objects need to sum on basis of key suffix

这是原始数组:

[
    {
      id: robin, 
      savings: 5500,
      cost: 1200
    },
    {
     id: robin_1,
     savings: 50,
     cost: 100
    },
    {
    id: robin_2,
     savings: 50,
     cost: 150
    },
    {
      id: steve, 
      savings: 100,
      cost: 1000
    },
    {
     id: steve_1,
     savings: 50,
     cost: 550
    },
    {
    id: steve_2,
     savings: 50,
     cost: 150
    },
]

我正在努力使其低于预期输出。

{
 robin :{
   allTime:{
      savings: 5500,
      cost: 1200,
    },
   today:{
     savings: 100,
     cost: 250 
    }
  },
  steve:{
   allTime: {
     savings:100,
     cost: 1000
   },
   today: {
     savings: 100,
     cost: 700
   }
  }
}

基本上当id只有robin的时候在allTime键中设置输出,当robin后面有一些后缀比如1,2,3然后将它们相加并设置为今天。

尝试过正常循环它们并使用 _.sumBy() 分组,如下所示,但失败了。

var output = _.groupBy(resultSet, value => value.id)
               .map((objs, key) => (
                { 'id': key,
                  'savings': _.sumBy(objs,'savings'), 
                  'cost': _.sumBy(objs, 'cost'))
                }
                .value();

我用 array#reduce 做到了:

let data = [
    { id: 'robin', savings: 5500, cost: 1200 },
    { id: 'robin_1', savings: 50, cost: 100 },
    { id: 'robin_2', savings: 50, cost: 150 },
    { id: 'steve', savings: 100, cost: 1000 },
    { id: 'steve_1', savings: 50, cost: 550 },
    { id: 'steve_2', savings: 50, cost: 150 },
];

let newData = data.reduce((acc, { id, savings, cost }) => {
    let [pre, suff] = id.split('_');

    if (!acc[pre]) acc[pre] = { allTime: {}, today: { savings: 0, cost: 0 } };
    if (!suff) acc[pre].allTime = { savings, cost };
    else (acc[pre].today.savings += savings), (acc[pre].today.cost += cost);

    return acc;
}, {});

console.log(newData);

像这样使用 for...of 循环:

let data = [
    { id: 'robin', savings: 5500, cost: 1200 },
    { id: 'robin_1', savings: 50, cost: 100 },
    { id: 'robin_2', savings: 50, cost: 150 },
    { id: 'steve', savings: 100, cost: 1000 },
    { id: 'steve_1', savings: 50, cost: 550 },
    { id: 'steve_2', savings: 50, cost: 150 },
];

let newData = {};
for ({ id, savings, cost } of data) {
    let [pre, suff] = id.split('_');

    if (!newData[pre]) newData[pre] = { allTime: {}, today: { savings: 0, cost: 0 } };
    if (!suff) newData[pre].allTime = { savings, cost };
    else (newData[pre].today.savings += savings), (newData[pre].today.cost += cost);
}

console.log(newData)

尝试通过 reduce

循环

const data = [
    {
      id: "robin", 
      savings: 5500,
      cost: 1200
    },
    {
     id: "robin_1",
     savings: 50,
     cost: 100
    },
    {
    id: "robin_2",
     savings: 50,
     cost: 150
    },
    {
      id: "steve", 
      savings: 100,
      cost: 1000
    },
    {
     id: "steve_1",
     savings: 50,
     cost: 550
    },
    {
    id: "steve_2",
     savings: 50,
     cost: 150
    },
];
    const res = data.reduce((acc, val) => {
          const splitVal =  val.id.split("_")[0];
          if(acc[val.id] || acc[splitVal]){
               acc[splitVal].today.savings+=val.savings;
               acc[splitVal].today.cost+=val.cost;
          }else{
               acc[val.id] = {
                    allTime:{
                         savings: val.savings,
                         cost: val.cost,
                    },
                    today:{
                         savings: 0,
                         cost: 0 
                    }
               }
          }
          return acc;
    }, {});
    console.log(res);

我想我会像这样选择 Array.prototype.reduce()

const input = [
  { id: 'robin', savings: 5500, cost: 1200 },
  { id: 'robin_1', savings: 50, cost: 100 },
  { id: 'robin_2', savings: 50, cost: 150 },
  { id: 'steve', savings: 100, cost: 1000 },
  { id: 'steve_1', savings: 50, cost: 550 },
  { id: 'steve_2', savings: 50, cost: 150 },
  { id: 'bob', savings: 1000, cost: 300 },
  { id: 'mark_1', savings: 50, cost: 150 }
];

const output = input.reduce((outObj, item) => {
  const { savings, cost } = item;
  const [mainKey, suffix] = item.id.split('_');
  const subKey = suffix === undefined ? 'allTime' : 'today';

  if (!outObj.hasOwnProperty(mainKey)) {
    outObj[mainKey] = {
      [subKey]: { savings, cost }
    }
  } else {
    if (!outObj[mainKey].hasOwnProperty(subKey)) {
      outObj[mainKey][subKey] = { savings, cost }
    } else {
      outObj[mainKey][subKey].savings += savings;
      outObj[mainKey][subKey].cost += cost;
    }
  }

  return outObj;
}, {})

//test
console.log(output);

使用这种方法,您无需将属性 allTimetoday“硬编码”到输出对象的每个条目中;相反,您仅动态添加它们 if/where 它们是需要的(请参阅带有 bobmark 的示例,其中仅添加 allTimetoday