John Doe John Doe - 1 month ago 19
JSON Question

Multiply the object properties

someone tell me how to multiply object's properties? I need this object multiplied by the count of property price and put together

var menu = [
{
"id": 5,
"price": 13,
"count": 2
},
{
"id": 8,
"price": 7,
"count": 3
},
{
"id": 9,
"price": 17,
"count": 1
}
]

var sum = 0;
for (var key in menu) {
for (var key1 in menu[key]) {
//console.log(key1);
if (key1 == 'price'){
price += menu[key][key1];
}

}

}


but I have no idea how to multiply count property

Answer

suppose you want to calculate the sum

var menu = [
    {
    "id": 5, "price": 13, "count": 2 }, {
    "id": 8, "price": 7, "count": 3 }, { "id": 9, "price": 17, "count": 1 } ];
    var sum = 0, item;
    for(item in menu)
    {
      sum += menu[item].price * menu[item].count;
    }
    console.log(sum);