0

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)
  },
4
  • What do you want the resulting array to look like? What have you tried so far? Commented Jul 18, 2018 at 22:29
  • What are you passing into getTotal() as arr? Each item is an array Commented Jul 18, 2018 at 22:35
  • The item arrays. Commented Jul 18, 2018 at 22:36
  • 1
    you should include the result you want to get. I'm having an issue interpreting your need. Commented Jul 18, 2018 at 22:38

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.