1

I want to get the total price of nested arrays in a specific category e.g: Hot Drinks.

Here is a sample of what I have now, so I want to filter out and get the total price of Hot Drinks Category only.

[
  {
    totalPrice: 30,
    _id: '6014fa4324e125599eaa72b5',
    orderItems: [
      {
        _id: '6014fa4324e125599eaa747ss',
        category: 'Breakfast',
        name: 'food name 1',
        price: 3,
        qty: 1,
      },
      {
        _id: '6014fa4324e125599eaa747s5',
        category: 'Hot Drinks',
        name: 'drink name 1',
        price: 3,
        qty: 5,
      },
      {
        _id: '6014fa4324e125599eaa74767',
        category: 'Hot Drinks',
        name: 'drink name 2',
        price: 4,
        qty: 2,
      },
    ],
  },
  {
    totalPrice: 23,
    _id: '6014fa4324e125599eaa7276e',
    orderItems: [
      {
        _id: '6014fa4324e125599eaa747ss',
        category: 'Hot Drinks',
        name: 'drink name 1',
        price: 3,
        qty: 6,
      },
    ],
  },
]

5 Answers 5

1

You can apply a filter method on the array and then just add the values on the filtered array. Something like below:

let prod = [
    {
      totalPrice: 30,
      _id: '6014fa4324e125599eaa72b5',
      orderItems: [
           {
             _id: '6014fa4324e125599eaa747ss',
             category: 'Breakfast',
             name: 'food name 1',
             price: 3,
             qty: 1,
           },
           {
             _id: '6014fa4324e125599eaa747s5',
             category: 'Hot Drinks',
             name: 'drink name 1',
             price: 3,
             qty: 5,
           },
           {
             _id: '6014fa4324e125599eaa74767',
             category: 'Hot Drinks',
             name: 'drink name 2',
             price: 4,
             qty: 2,
           },
           ],
         },
         {
            totalPrice: 23,
            _id: '6014fa4324e125599eaa7276e',
            orderItems: [
              {
                _id: '6014fa4324e125599eaa747ss',
                category: 'Hot Drinks',
                name: 'drink name 1',
                price: 3,
                qty: 6,
              },
           ],
         },
       ];


  function getPriceByCategory(category, products) {
     let price = 0;

     products.forEach(orders => {
       orders.orderItems.filter(order => order.category == category).forEach(item => {
          price += item.price;
       });
     });

     return price;
  }

  const totalPrice = getPriceByCategory('Hot Drinks', prod);
  alert(totalPrice);

Sample JS Fiddle: https://jsfiddle.net/sagarag05/qwzju53f/9/

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

Comments

1
const filterBy = 'Hot Drinks';

const items = [
  {
    totalPrice: 30,
    _id: '6014fa4324e125599eaa72b5',
    orderItems: [
      {
        _id: '6014fa4324e125599eaa747ss',
        category: 'Breakfast',
        name: 'food name 1',
        price: 3,
        qty: 1,
      },
      {
        _id: '6014fa4324e125599eaa747s5',
        category: 'Hot Drinks',
        name: 'drink name 1',
        price: 3,
        qty: 5,
      },
      {
        _id: '6014fa4324e125599eaa74767',
        category: 'Hot Drinks',
        name: 'drink name 2',
        price: 4,
        qty: 2,
      },
    ],
  },
  {
    totalPrice: 23,
    _id: '6014fa4324e125599eaa7276e',
    orderItems: [
      {
        _id: '6014fa4324e125599eaa747ss',
        category: 'Hot Drinks',
        name: 'drink name 1',
        price: 3,
        qty: 6,
      },
    ],
  },
]
const sumOf = (items, filterBy) => {
  let totalPrice = 0;
  items.forEach(item => {
    item.orderItems.forEach(orderItem => {
      if (orderItem.category === filterBy) {
        totalPrice += orderItem.price;
      }
    })
  })
  return totalPrice;
}

console.log(sumOf(items, filterBy))

Comments

1
let sum = 0;
allOrders.forEach(order => {
order.orderItems.forEach(item => { 
if(item.category=='Hot Drinks') {
    sum+ = item.price * item.qty
}});
});

sum has the total price for Hot Drinks

Comments

1

Assuming you named that information as data:

  1. Generate a big array of all the "orderItems"
  2. For each of those elements sum the price if the category is "Hot Drinks"
const totalPrice = data
    .reduce((acc, { orderItems }) => [...acc, ...orderItems], [])
    .reduce((acc, { category, price }) => category === "Hot Drinks" ? acc + price : acc, 0);

console.log(totalPrice);  // 10

Comments

1

Use flatMap and reduce or alternatively using forEach and destructuring

const total = (arr, text) =>
  arr
    .flatMap(({ orderItems }) => orderItems)
    .reduce((acc, { category, price }) => 
      (acc + (category === text ? price : 0)), 0);

// alternatively
const total2 = (arr, text, acc = 0) => {
  arr.forEach(({ orderItems }) =>
    orderItems.forEach(
      ({ category, price }) => (category === text && (acc += price))
    )
  );
  return acc;
};

const data = [
  {
    totalPrice: 30,
    _id: "6014fa4324e125599eaa72b5",
    orderItems: [
      {
        _id: "6014fa4324e125599eaa747ss",
        category: "Breakfast",
        name: "food name 1",
        price: 3,
        qty: 1,
      },
      {
        _id: "6014fa4324e125599eaa747s5",
        category: "Hot Drinks",
        name: "drink name 1",
        price: 3,
        qty: 5,
      },
      {
        _id: "6014fa4324e125599eaa74767",
        category: "Hot Drinks",
        name: "drink name 2",
        price: 4,
        qty: 2,
      },
    ],
  },
  {
    totalPrice: 23,
    _id: "6014fa4324e125599eaa7276e",
    orderItems: [
      {
        _id: "6014fa4324e125599eaa747ss",
        category: "Hot Drinks",
        name: "drink name 1",
        price: 3,
        qty: 6,
      },
    ],
  },
];

console.log(total(data, 'Hot Drinks'))
console.log(total2(data, 'Hot Drinks'))

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.