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??
6 Answers 6
cart is not an array in your case it is an object, this would work in your case
var carts = [];
for (i = 0; i < len; i++) {
var cart = {};
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;
carts.push(cart);
}
console.log(carts);
Comments
Firstly to declare cart as an array, you need to use [], then you are replacing the information inside the object in each iteration, so only last iteration is effective. you need to do something like this:
var cart = [];
for (i = 0; i < len; i++) {
var temp = {};
temp.item_name = items[i].get("item_name");
temp.quantity = items[i].get("quantity");
temp.amount = items[i].get("amount");
temp.total = cart.amount * cart.quantity;
temp.subtotal = cart.subtotal + cart.total;
cart.push(temp);
}
console.log(cart);
Comments
Declare an array and place the individuals cart inside it.
let carts = [];
for (let i = 0; i < len; i++) {
let cart = {};
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;
carts.push(cart);
}
console.log(carts);
Comments
Use an Array rather than an Object and add each cart Object in this array with the push() method.
var carts = [];
for (i = 0; i < len; i++) {
var cart = {};
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;
carts.push(cart);
}
Comments
I think you want an array of objects, you just have an object.
var cartItems = [];
var cartItem;
for (var i = 0; i < len; i++) {
cartItem = {};
cartItem.item_name = items[i].get("item_name");
cartItem.quantity = items[i].get("quantity");
cartItem.amount = items[i].get("amount");
cartItem.total = cartItem.amount * cartItem.quantity;
cartItem.subtotal = cartItem.subtotal + cartItem.total;
cartItems.push(cartItem);
}
console.log(cartItems);
Comments
You didn't declare cart as an array. Now the syntax for array would be like this
var cart = [];
and then declare the object in the loop like this
for (i = 0; i < len; i++) {
var cart = {};
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;
carts.push(cart);
}
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?