I have the following code:
var cart = {};
for (i = 0; i < len; i++) {
cart.item_name = items[i].get("item_name");
cart.quantity = items[i].get("quantity");
cart.amount = items[i].get("amount");
cart.total = cart.amount * cart.quantity;
cart.subtotal = cart.subtotal + cart.total;
}
console.log(cart);
I would like the data item_name,quantity,amount,total,subtotal to be stored in the array cart during each loop. However only the data in the last loop is being displayed in console. Why is this and why is not all the data stored in the array??
cartis not an array, it's an object in your casecartis a object and you are overriding every time while iterating, so its resulting the last one.itemsarray instead a copy of it?