2

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.

xGeo
2,1492 gold badges21 silver badges47 bronze badges
asked Jul 16, 2021 at 16:44

6 Answers 6

3

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

answered Jul 16, 2021 at 16:47
Sign up to request clarification or add additional context in comments.

Comments

2

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

answered Jul 16, 2021 at 16:51

Comments

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

answered Jul 16, 2021 at 16:52

Comments

1

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);
answered Jul 16, 2021 at 16:56

Comments

1

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

answered Jul 16, 2021 at 16:51

Comments

0

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)
answered Jul 16, 2021 at 16:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.