简体   繁体   English

如何使用 array.reduce 返回新数组

[英]how to return new array by using array.reduce

I want to return new array by using reduce.我想使用reduce返回新数组。 For example,例如,

const product = [
  { color: 'orange', type: 'hat', count: 1 },
  { color: 'orange', type: 'hat', count: 1 },
  { color: 'orange', type: 'shoes', count: 1 },
  { color: 'blue', type: 'food', count: 1 },
];

the product list need to like below because there are two 'hat' therefore, the count should be 2 and one { color: 'orange', type: 'hat', count: 1 } should be removed.产品列表需要如下所示,因为有两个“帽子”,因此计数应为 2,并且应删除一个 { color: 'orange', type: 'hat', count: 1 }。

const result = product.reduce((acc, curr) => {
// I want to make new array like
// const product = [
//  { color: 'orange', type: 'hat', count: 2 },
//  { color: 'orange', type: 'shoes', count: 1 },
//  { color: 'blue', type: 'food', count: 1 },
//];
 return acc
}

thank you!谢谢你!

I would do it the following way :我会这样做:

  • First find if the product has already been added to the curr array首先查找产品是否已经添加到curr数组中
  • If yes, increments the count如果是,则增加count
  • else, push the new product into the array否则,将新产品推入数组

Note : I've used the Spread operator to make a deep copy of the object instead of pushing the current object into the array.注意:我使用了Spread 运算符来制作对象的深层副本,而不是将当前对象推送到数组中。

This result by not modifying the products array and create a completely new array这个结果通过不修改products数组并创建一个全新的数组

 const product = [ { color: 'orange', type: 'hat', count: 1 }, { color: 'orange', type: 'hat', count: 1 }, { color: 'orange', type: 'shoes', count: 1 }, { color: 'blue', type: 'food', count: 1 }, ]; const result = product.reduce((acc, curr) => { if(!acc) return [...curr] const exist = acc.find(x => x.type === curr.type && x.color === curr.color) exist ? exist.count += 1 : acc.push({...curr}) return acc }, []) console.log(result)

You can try it你可以试试

 const product = [ { color: 'orange', type: 'hat', count: 1 }, { color: 'orange', type: 'hat', count: 1 }, { color: 'orange', type: 'shoes', count: 1 }, { color: 'blue', type: 'food', count: 1 }, ]; const result = product.reduce((res, obj) => { let exist = res.find(o => o.color === obj.color && o.type === obj.type) if (exist) { exist.count += obj.count } else { res = [...res, {...obj}] } return [...res] }, []) console.log(result) console.log(product)

Update Edit with suggestion by yassine-el-bouchaibi使用yassine-el-bouchaibi 的建议更新编辑

From the above comment ...从上面的评论...

The task also could be described as grouping, merging and aggregating .该任务也可以描述为分组、合并和聚合 It is a quite common task and can be solved by a generically implemented but customizable reducer function ... see ... "How to group and merge array entries and to sum-up values on multiple common (but not all) keys?"这是一项非常常见的任务,可以通过通用实现但可自定义的 reducer 函数来解决……请参阅…… “如何对数组条目进行分组和合并,并对多个常见(但不是全部)键上的值求和?”

... prove ... ... 证明 ...

 function groupMergeAndAggregateGenerically(collector, item) { const { createKey, createMerger, aggregate, lookup, result = [], } = collector; const key = createKey(item); let merger = lookup.get(key) ?? null; if (merger === null) { merger = createMerger(item); lookup.set(key, merger); result.push(merger); } aggregate(merger, item); return collector; } const productData = [ { color: 'orange', type: 'hat', count: 1 }, { color: 'orange', type: 'hat', count: 1 }, { color: 'orange', type: 'shoes', count: 1 }, { color: 'blue', type: 'food', count: 1 }, ]; const { result: mergedData } = productData .reduce(groupMergeAndAggregateGenerically, { createKey: ({ color, type }) => [color, type].join('_'), createMerger: ({ color, type }) => ({ color, type, count: 0 }), aggregate: (merger, { count }) => merger.count += count, lookup: new Map, result: [], }); console.log({ mergedData, productData });
 .as-console-wrapper { min-height: 100%!important; top: 0; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM