2

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??

7
  • 2
    cart is not an array, it's an object in your case Commented Aug 6, 2017 at 16:38
  • 1
    cart is a object and you are overriding every time while iterating, so its resulting the last one. Commented Aug 6, 2017 at 16:39
  • @CommercialSuicide how come?? and does this mean it cannot store data Commented Aug 6, 2017 at 16:39
  • @KoushikChatterjee okay so what should I do? Commented Aug 6, 2017 at 16:40
  • stote in another array as most of the answers says. btw, why can't you directly use items array instead a copy of it? Commented Aug 6, 2017 at 16:41

6 Answers 6

3

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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

1

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

1

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

0

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

0

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.