admin管理员组

文章数量:1400400

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

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

Share Improve this question asked Aug 6, 2017 at 16:36 lorrainemutheulorrainemutheu 1152 silver badges17 bronze badges 7
  • 2 cart is not an array, it's an object in your case – P.S. 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. – Koushik Chatterjee Commented Aug 6, 2017 at 16:39
  • @CommercialSuicide how e?? and does this mean it cannot store data – lorrainemutheu Commented Aug 6, 2017 at 16:39
  • @KoushikChatterjee okay so what should I do? – lorrainemutheu 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? – Koushik Chatterjee Commented Aug 6, 2017 at 16:41
 |  Show 2 more ments

6 Answers 6

Reset to default 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);

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

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

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

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

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

本文标签: Store data in an array using for loop javascriptStack Overflow