我不想渲染重复项目并在 item_number 中显示有多少重复项目,但地图不这样做
I do not want duplicate items to be rendered and to show in item_number how many duplicate items there are but the map does not do this
{arrOfProducts.map((项目, 索引) => {
return (
);
}, 0)}
;
数组有方法reduce
arrOfProducts.reduce((acc, curr) => {
const foundedIndex = acc.findIndex(i => curr.id === i.id)
if(foundedIndex !== -1){
const item = acc[foundedIndex]
return [...acc.slice(0, foundedIndex), {...item, count: item.count+1}, ...acc.slice(foundedIndex+1)]
}
return [...acc, {...curr, count: 1}]
}, [])
数组有方法reduce
arrOfProducts.reduce((acc, curr) => {
const foundedIndex = acc.findIndex(i => curr.id === i.id)
if(foundedIndex !== -1){
const item = acc[foundedIndex]
return [...acc.slice(0, foundedIndex), {...item, count: item.count+1}, ...acc.slice(foundedIndex+1)]
}
return [...acc, {...curr, count: 1}]
}, [])