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??
-
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
6 Answers
Reset to default 3cart 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
版权声明:本文标题:Store data in an array using for loop javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744207543a2595260.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论