I'm trying to walk through a sorted array and count the duplicates to create a new summary-array.
for (var i = 0; i < newCart.length; i++) {
//if new item.
console.log(JSON.stringify(newCart[i - 1]))
if ( JSON.stringify(newCart[i - 1]) !== JSON.stringify(newCart[i])) {
//add to new displayed items in cart
results.push(newCart[i]);
results[results.length - 1].count = 1
}else{
console.log('second')
//add one to the count.
results[results.length - 1].count++;
}
}
When I'm looping through this with three items, I get the following output:
undefined
{"id":1,"name":"Skateboard","price":1299,"currency":"SEK","image":"/static/img/products/1.jpg","thumbnail":"/static/img/products/1-t.jpg","description":"This board is the boss!","count":1} main.js:47
{"id":1,"name":"Skateboard","price":1299,"currency":"SEK","image":"/static/img/products/1.jpg","thumbnail":"/static/img/products/1-t.jpg","description":"This board is the boss!","count":1}
How is it possible that the count variable ends up in the newCart-array?