lorrainemutheu
lorrainemutheu

Reputation: 115

Store data in an array using for loop javascript

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

Upvotes: 2

Views: 4887

Answers (6)

viral gandhi
viral gandhi

Reputation: 56

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

Upvotes: 0

davidxxx
davidxxx

Reputation: 131326

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

Upvotes: 1

marvel308
marvel308

Reputation: 10458

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

Upvotes: 3

Jeremy Jackson
Jeremy Jackson

Reputation: 2257

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

Upvotes: 0

Hassan Imam
Hassan Imam

Reputation: 22524

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

Upvotes: 1

Dij
Dij

Reputation: 9808

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

Upvotes: 1

Related Questions