【发布时间】:2022-01-10 03:00:08
【问题描述】:
【问题讨论】:
-
那么你的代码有什么问题?您是如何尝试调试问题的?
-
我不希望重复项目被渲染并在 item_number 中显示有多少重复项目,但地图不这样做
标签: javascript arrays reactjs object developer-tools
【问题讨论】:
标签: javascript arrays reactjs object developer-tools
数组有方法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}]
}, [])
【讨论】: