Arrays has method 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}]
}, [])