How do I get the total sum of nested arrays in Reactjs?

Ahmad Ebrahim

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,
      },
    ],
  },
]
Sagar Agrawal

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/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I add 2 arrays together and get the average of the sum?

From Dev

How do I Sum a total based on Grouping

From Dev

In rails, how do you total the values of a hash if the values are nested arrays

From Dev

How do I count the sum of specified sum in $total

From Dev

How do i get the total average?

From Dev

How do I sum total amount of array in loop?

From Dev

How do I sum total amount of array in loop?

From Dev

How do I get a sum of these numbers in array?

From Dev

How do I get the SUM visible?

From Dev

How do I get the sum of all the properties?

From Dev

How can I get a sum of arrays of tuples in scala

From Dev

How do I get such a nested lists?

From Dev

How do I get such a nested lists?

From Dev

How do I get the json nested object?

From Dev

How Do I Get Out of Nested Loop?

From Dev

How do I find an object nested deep in arrays in MongoDB?

From Dev

How to get the total sum of a column and get total records at the same time

From Dev

How do I get the total number of distinct values in a column in a CSV?

From Dev

How do I get the last iteration of foreach if the total is unknown

From Dev

How do I get total text and voice channel count in numbers

From Dev

How do I get a single total of lines with `wc -l`?

From Dev

How do I get total Qty using one linq query?

From Dev

How do I get total number of objects in a variable only once?

From Dev

How do I get the total size of everything in a directory in one line?

From Java

why I do not get my total count of a column with sum function in R?

From Java

How to aggregate sum in MongoDB to get a total count?

From Dev

How to get total Sum of my Collection/Class

From Dev

How to get the total sum for column with joins?

From Dev

How to get the total sum of a column in jqgrid

Related Related

  1. 1

    How do I add 2 arrays together and get the average of the sum?

  2. 2

    How do I Sum a total based on Grouping

  3. 3

    In rails, how do you total the values of a hash if the values are nested arrays

  4. 4

    How do I count the sum of specified sum in $total

  5. 5

    How do i get the total average?

  6. 6

    How do I sum total amount of array in loop?

  7. 7

    How do I sum total amount of array in loop?

  8. 8

    How do I get a sum of these numbers in array?

  9. 9

    How do I get the SUM visible?

  10. 10

    How do I get the sum of all the properties?

  11. 11

    How can I get a sum of arrays of tuples in scala

  12. 12

    How do I get such a nested lists?

  13. 13

    How do I get such a nested lists?

  14. 14

    How do I get the json nested object?

  15. 15

    How Do I Get Out of Nested Loop?

  16. 16

    How do I find an object nested deep in arrays in MongoDB?

  17. 17

    How to get the total sum of a column and get total records at the same time

  18. 18

    How do I get the total number of distinct values in a column in a CSV?

  19. 19

    How do I get the last iteration of foreach if the total is unknown

  20. 20

    How do I get total text and voice channel count in numbers

  21. 21

    How do I get a single total of lines with `wc -l`?

  22. 22

    How do I get total Qty using one linq query?

  23. 23

    How do I get total number of objects in a variable only once?

  24. 24

    How do I get the total size of everything in a directory in one line?

  25. 25

    why I do not get my total count of a column with sum function in R?

  26. 26

    How to aggregate sum in MongoDB to get a total count?

  27. 27

    How to get total Sum of my Collection/Class

  28. 28

    How to get the total sum for column with joins?

  29. 29

    How to get the total sum of a column in jqgrid

HotTag

Archive