JS根据其属性之一减少列表

时间:2019-01-24 17:17:00

标签: javascript arrays reactjs list reduce

我正在用react js编写一种方法,说我有一个像下面的列表。

const itemList = [
    {
        "id": 3295042,
        "tillPoint": "A",
        "date": "23/01/2019",
        "AnnaIncome": 100,
        "PeterIncome": 50,
        "KenIncome": 100,
        "freeCash": 30
    },
    {
        "id": 3295042,
        "tillPoint": "B",
        "date": "23/01/2019",
        "AnnaIncome": 300,
        "PeterIncome": 200,
        "KenIncome": 100,
        "freeCash": 50
    },
    {
        "id": 3295042,
        "tillPoint": "A",
        "date": "22/01/2019",
        "AnnaIncome": 120,
        "PeterIncome": 120,
        "KenIncome": 30,
        "freeCash": 50
    },
    {
        "id": 3295042,
        "tillPoint": "B",
        "date": "22/01/2019",
        "AnnaIncome": 100,
        "PeterIncome": 150,
        "KenIncome": 50,
        "freeCash": 60
    },
] 

我希望执行.reduce,以使列表仅按日期属性包含较少的项。 例如,在上面的列表中,实际上只有两个日期22/01/2019和23/01/2019。 我希望按每个对象的日期缩小列表的大小。因此,每个人的收入将以自己的名字加在一起。 最终,列表将仅成为2个对象。并且在新列表中删除了一些属性 例如,在下面,删除了“ id”和“ tillpoint”,其余的属性加在一起:

let itemList = [
    {
        "date": "23/01/2019",
        "AnnaIncome": 300,
        "PeterIncome": 250,
        "KenIncome": 200,
        "freeCash": 80
    },
    {
        "date": "22/01/2019",
        "AnnaIncome": 220,
        "PeterIncome": 280,
        "KenIncome": 80,
        "freeCash": 110
    }
]

我在这里尝试过的操作如下所示,但它不起作用。

itemList.reduce((a, b) => ({ date: a.AnnaIncome + b.AnnaIncome, a.PeterIncome + b.PeterIncome, a.KenIncome + b.KenIncome, a.freeCash + b.freeCash }));

3 个答案:

答案 0 :(得分:2)

您必须按日期分组,这可以使用哈希表轻松完成:

 const byDate = new Map;

 for(const { date, id, tillPoint, ...rest } of itemList) {
   if(byDate.has(date)) {
     const dupe = byDate.get(date);
     for(const [k, v] of Object.entries(rest))
       dupe[k] += v;
   } else {
     byDate.set(date, { date, ...rest });
   }
}

const result = [...byDate.values()];

如果性能非常重要,请确保可以轻松将添加的内容硬编码为dupe.something += rest.something

答案 1 :(得分:1)

您可以使用reducespread运算符。

所以我在这里要做的就是获取要直接添加到输出中的动态属性,而无需在...rest中进行任何操作,而剩下的就是我要取出各自的名称。

然后,我检查数据密钥是否已存在,然后添加...rest并将freeCash添加到现有值。如果不存在,我们将使用日期和相应的值创建一个新密钥。

const itemList = [{"id":3295042,"tillPoint":"A","date":"23/01/2019","AnnaIncome":100,"PeterIncome":50,"KenIncom":100,"freeCash":30},{"id":3295042,"tillPoint":"B","date":"23/01/2019","AnnaIncome":300,"PeterIncome":200,"KenIncom":100,"freeCash":50},{"id":3295042,"tillPoint":"A","date":"22/01/2019","AnnaIncome":120,"PeterIncome":120,"KenIncom":30,"freeCash":50},{"id":3295042,"tillPoint":"B","date":"22/01/2019","AnnaIncome":100,"PeterIncome":150,"KenIncom":50,"freeCash":60},]

const op = itemList.reduce((output,current)=>{
  let {id,tillPoint,date,freeCash,...rest} = current
 
  if(output[date]){
    output[date] = {
      ...output[date],
      ...rest,
      date,
      freeCash: output[date].freeCash +freeCash
    }
  } else {
    output[date] ={
      ...rest,
      date,
      freeCash
    }
  }
  return output
},{})

console.log(Object.values(op))

答案 2 :(得分:1)

也许是这样的:

const itemList = [
    { "id": 3295042, "tillPoint": "A", "date": "23/01/2019", "AnnaIncome": 100, "PeterIncome": 50,  "KenIncome": 100, "freeCash": 30 },
    { "id": 3295042, "tillPoint": "B", "date": "23/01/2019", "AnnaIncome": 300, "PeterIncome": 200, "KenIncome": 100, "freeCash": 50 },
    { "id": 3295042, "tillPoint": "A", "date": "22/01/2019", "AnnaIncome": 120, "PeterIncome": 120, "KenIncome": 30,  "freeCash": 50 },
    { "id": 3295042, "tillPoint": "B", "date": "22/01/2019", "AnnaIncome": 100, "PeterIncome": 150, "KenIncome": 50,  "freeCash": 60 },
];


var new_itemList=[];

for(var key in itemList){
	var new_key = -1;
	for(var keyn in new_itemList){
	   if(new_itemList[keyn].date == itemList[key].date){
	   	  new_key = parseInt(keyn, 10);
	   }
	}
	if(new_key == -1){
		new_itemList.push({'date':itemList[key].date, 'AnnaIncome': itemList[key].AnnaIncome, 'PeterIncome': itemList[key].PeterIncome, 'KenIncome': itemList[key].KenIncome, 'freeCash': itemList[key].freeCash});
	}else{
       new_itemList[new_key].AnnaIncome +=  itemList[key].AnnaIncome;
       new_itemList[new_key].PeterIncome +=  itemList[key].PeterIncome;
       new_itemList[new_key].KenIncome +=  itemList[key].KenIncome;
       new_itemList[new_key].freeCash +=  itemList[key].freeCash;
	}
}
console.log(new_itemList);