robertkovacs
robertkovacs

Reputation: 465

Summarize objects of array

I have an order array. I don't see how could I summarize the orders with price and quantity. I wanted to get the prices and quantity from item object and summing it, but how could do that?

{
  "orders" : [ null, {
    "date" : "2018-07-09 10:07:18",
    "item" : [ {
      "name" : "Warmer Topfenstrudel",
      "price" : 3.9,
      "quantity" : 1
    } ]
  }, {
    "date" : "2018-07-09 10:07:30",
    "item" : [ {
      "name" : "Warmer Topfenstrudel",
      "price" : 3.9,
      "quantity" : 1
    } ]
  }, {
    "date" : "2018-07-09 15:07:18",
    "item" : [ {
      "name" : "Piccata Milanese",
      "price" : 12.9,
      "quantity" : 3
    } ]
  }, {
    "date" : "2018-06-13 10:07:18",
    "item" : [ {
      "name" : "Wiener Schnitzel vom Schwein",
      "price" : 9.9,
      "quantity" : 2
    } ]
  } ]
}

I tried following function:

getTotal: function(arr) {
    return arr.reduce((sum, i) => {
      return sum + (i.price * i.quantity)
    },0)
  },

Upvotes: 0

Views: 183

Answers (3)

Kosta B.
Kosta B.

Reputation: 271

I wanted to get the prices and quantity from item object and summing it, but how could do that?

One of the ways to do that:

var j = {
  "orders": [null, {
    "date": "2018-07-09 10:07:18",
    "item": [{
      "name": "Warmer Topfenstrudel",
      "price": 3.9,
      "quantity": 1
    }]
  }, {
    "date": "2018-07-09 10:07:30",
    "item": [{
      "name": "Warmer Topfenstrudel",
      "price": 3.9,
      "quantity": 1
    }]
  }, {
    "date": "2018-07-09 15:07:18",
    "item": [{
      "name": "Piccata Milanese",
      "price": 12.9,
      "quantity": 3
    }]
  }, {
    "date": "2018-06-13 10:07:18",
    "item": [{
      "name": "Wiener Schnitzel vom Schwein",
      "price": 9.9,
      "quantity": 2
    }]
  }]
}
var ar = j.orders,
  sum = 0;
// iterates over the object array keys 
Object.keys(ar).forEach(function(i) {
  // Summing up the price * quantity
  sum += ar[i] === null ? 0 : (ar[i].item[0].price * ar[i].item[0].quantity)
  console.log(sum);
})

Upvotes: 1

Tholle
Tholle

Reputation: 112797

item is an array, so you need to use reduce on that as well, or use the first element of the array if you know that there will only be one item.

You also need have a check if the element in the array is an actual order, since you have null in your array.

var obj = {
  orders: [
    null,
    {
      date: "2018-07-09 10:07:18",
      item: [
        {
          name: "Warmer Topfenstrudel",
          price: 3.9,
          quantity: 1
        }
      ]
    },
    {
      date: "2018-07-09 10:07:30",
      item: [
        {
          name: "Warmer Topfenstrudel",
          price: 3.9,
          quantity: 1
        }
      ]
    },
    {
      date: "2018-07-09 15:07:18",
      item: [
        {
          name: "Piccata Milanese",
          price: 12.9,
          quantity: 3
        }
      ]
    },
    {
      date: "2018-06-13 10:07:18",
      item: [
        {
          name: "Wiener Schnitzel vom Schwein",
          price: 9.9,
          quantity: 2
        }
      ]
    }
  ]
};

var result = obj.orders.reduce((result, order) => {
  return !order
    ? result
    : result + order.item.reduce((cost, item) => {
        return cost + item.price * item.quantity;
      }, 0);
}, 0);

console.log(result);

Upvotes: 1

peteb
peteb

Reputation: 19428

The problem is that you need to sum up the item array for each orders element before you can sum across all orders

getTotal: function(arr) {
    return arr.reduce((sum, order) => {
      return sum + order.items.reduce((itemSum, item) => (
        itemSum + (item.price * item.quantity)
      ), 0)
    },0)
  },

Upvotes: 1

Related Questions