For Each: forEach(function(singleItem) {
});forEach()
is used to run a function on every item in an array.
var priceList = [345, 566, 23, 45, 12];
var totalPrice = 0;
priceList.forEach(function(singlePrice) {
console.log('Single price:', singlePrice);
totalPrice = totalPrice + singlePrice
});
console.log(totalPrice); // 991
var items = [
{
"index": 1,
"item_id": "41499884552324",
"product_id": "7135045746820",
"variant_id": "41499884552324",
"item_name": "7 Shakra Bracelet",
"quantity": 2,
"price": 5400,
"discount": 0,
"item_category": "Bracelet",
"item_brand": "Company 123",
"item_variant": "Blue"
},
{
"index": 2,
"item_id": "41499881439364",
"product_id": "7135044173956",
"variant_id": "41499881439364",
"item_name": "Black Leather Bag",
"quantity": 1,
"price": 3800,
"discount": 0,
"item_brand": "partners-demo"
}
];
var totalQuantity = 0;
items.forEach(function(singleItem) {
totalQuantity = totalQuantity + singleItem.quantity;
});
console.log(totalQuantity); // total quantity