Adityo Fauzaan
Adityo Fauzaan

Reputation: 65

Reduce array inside of array

So I have this array :

const filteredArray2 = [
  [{
      'item': "Chocolate Waffle",
      'price': 5000
    },
    {
      'item': "Strawberry Waffle",
      'price': 6000
    }
  ],
  [{
    'item': "Chocolate Waffle",
    'price': 5000
  }]
];

And I want to get grand total of 16000, all I know is that I can use array.reduce but I don't know how to do It with this one.

Upvotes: 2

Views: 134

Answers (6)

Tushar Shahi
Tushar Shahi

Reputation: 20441

You can use a nested .reduce(). Reduce returns an aggregated value by doing calculations over an array.

The inner reduce can sum the sub array. Then you can add the inner result to your outer accumulated value in each step.

const filteredArray2 = [
  [
    {
      'item': "Chocolate Waffle",
      'price': 5000
    },
    {
      'item': "Strawberry Waffle",
     'price': 6000
    }
  ],
  [
    {
      'item': "Chocolate Waffle",
      'price': 5000
    }
  ]
]

let ans = filteredArray2.reduce((acc,a) => {
acc += a.reduce((acc,x) => acc+x.price,0); 
return acc;
},0)

console.log(ans);

Upvotes: 1

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48610

You can combine flatMap with reduce.

const filteredArray = [[
  { item: "Chocolate Waffle"  , price: 5000 },
  { item: "Strawberry Waffle" , price: 6000 }
], [
  { item: "Vanilla Waffle"    , price: 5000 }
]];

const grandTotal = filteredArray
  .flatMap(items => items.map(({ price }) => price))
  .reduce((total, price) => total + price, 0);

console.log(grandTotal);

Alternatively, you can flatten first.

const filteredArray = [[
  { item: "Chocolate Waffle"  , price: 5000 },
  { item: "Strawberry Waffle" , price: 6000 }
], [
  { item: "Vanilla Waffle"    , price: 5000 }
]];

const grandTotal = filteredArray
  .flat().reduce((total, { price }) => total + price, 0);

console.log(grandTotal);

You could also reduce recursively:

const filteredArray = [[
  { item: "Chocolate Waffle"  , price: 5000 },
  { item: "Strawberry Waffle" , price: 6000 }
], [
  { item: "Vanilla Waffle"    , price: 5000 }
]];

const calculateTotal = (list, visitor, result = 0) => {
  if (Array.isArray(list)) {
    return list.reduce((acc, item) =>
      acc + calculateTotal(item, visitor, result), result);
  } else if (list != null) {
    return visitor(list);
  } else {
    return result;
  }
}

const grandTotal = calculateTotal(filteredArray, ({ price }) => price);

console.log(grandTotal);

Upvotes: 2

Itai Pendler
Itai Pendler

Reputation: 65

great question. reducer can be a very confusing method to use, but it can get very simplified if you think about it like this: you have an array and a value you want to run an operation on all elements and get a single value (that value can be of whatever type you want).

now in order to get the value you want, you first need to flat your array of arrays to make it easier to work with, for this use the js .flat() method.

now that you have a single array that should look like so:

const flattenedArray = [
  {
    item: "Chocolate Waffle"
      price: 5000
  },
  {
    item: "Strawberry Waffle"
      price: 6000
  },
  {
    item: "Chocolate Waffle"
      price: 5000
  }
]

you can simply use the reduce method like so to get the total:

const total = flattedArray.reduce((prev, curr) => prev + curr.price, 0);

Upvotes: 1

Kinglish
Kinglish

Reputation: 23654

You can flat() the multidimensional array first, then reduce

const filteredArray = [
  [{ item: "Chocolate Waffle", price: 5000},{item: "Strawberry Waffle", price: 6000}],
  [{ item: "Chocolate Waffle", price: 5000}]]
let total = filteredArray.flat().reduce((b, a) => b + +a.price, 0);
console.log(total);

Upvotes: 3

Mohamed Farid
Mohamed Farid

Reputation: 131

you need to use two reducers inside of each other


const filteredArray = [
  [
    {
      item: "Chocolate Waffle",
      price: 5000
    },
    {
      item: "Strawberry Waffle",
      price: 6000
    }
  ],
  [
    {
      item: "Chocolate Waffle",
      price: 5000
    }
  ]
]
const res = filteredArray.reduce(
  (accumulator, currentValue)=>{
   return accumulator + currentValue.reduce((accumulator, currentValue) =>{
            return  currentValue.price + accumulator
    }, 0)
  },0)

Upvotes: 0

AlexSp3
AlexSp3

Reputation: 2293

  • Just iterate the first array and inside each nested array reduce it and add it to the total variable:

const filteredArray = [
  [{
      item: "Chocolate Waffle",
      price: 5000
    },
    {
      item: "Strawberry Waffle",
      price: 6000
    }
  ],
  [{
    item: "Vanilla Waffle",
    price: 5000
  }]
]

let total = 0;
filteredArray.forEach(e => {
  total += e.reduce((a, b) => a + b.price, 0);
});

console.log(total);

Upvotes: 1

Related Questions