0

I am trying to find the total from objects inside an array, which each object has a price and quantity, i can find the total when the array has exactly two objects, but for more than two it produces NaN.

arr = [ { quantity: 1, price: 30 },
{ quantity: 1, price: 40 },
{ quantity: 2, price: 10 },
{ quantity: 1, price: 10 } ]


const reducer = (accumulator, currentValue) => {
    var a = accumulator.quantity * accumulator.price;
    var b = currentValue.quantity * currentValue.price;
    return a + b;
}
console.log(arr.reduce(reducer));  // sum if array contains 2 objects, NaN otherwise.

3
  • accumulator won't have a quantity or price properties, since it's the result of the last return value. Since you do a + b, you'd only get a number the first time, but on the second iteration, you'll have accumulator = <previous>a + <previous>b Commented Oct 22, 2019 at 16:59
  • Check out the docs for reduce, specifically what it used as the initial accumulator value if none is provided. Commented Oct 22, 2019 at 17:00
  • @DaveNewton the initial is OK, in this case. But only the initial. There is a problem for anything other than two items. Commented Oct 22, 2019 at 17:01

3 Answers 3

2
    let arr = [ 
    { quantity: 1, price: 30 },
    { quantity: 1, price: 40 },
    { quantity: 2, price: 10 },
    { quantity: 1, price: 10 } 
    ]

    let reducer = (acc, cur) => {
     return acc + (Number(cur.quantity) * Number(cur.price));
    };

    console.log(arr.reduce(reducer, 0));
    // 100

Your reducer function seems to be wrong. Accumulator no longer has any parameters to it, since well, it accumulates - its an integer. Also, set a initial value for your accumulator to start accumulating from, as shown in the reduce function, second parameter input

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

3 Comments

Why explicitly convert the numbers to numbers?
Just in case somewhere in the input object, the value is stored as string
None are here, so it's a bit useless to think of this case. And what if the value is a boolean? And what if it's undefined? What if the value is a string but it's "apple"? Not really worth it solving all these potential issues. Not to mention that the * operator will do an implicit casting to number for both operands. Even if one of the values is the string "10" instead of the number 10 that will be handled. And you'll still have the same problems with non-numeric strings and other types.
0
arr = [ { quantity: 1, price: 30 },
{ quantity: 1, price: 40 },
{ quantity: 2, price: 10 },
{ quantity: 1, price: 10 } ]

const reducer = (accumulator, currentValue) {
    return accumulator + (currentValue.quantity * accumulator.price);
}

console.log(arr.reduce(reducer, 0 ));

Comments

-1

you can simply say

arr = [ { quantity: 1, price: 30 },
{ quantity: 1, price: 40 },
{ quantity: 2, price: 10 },
{ quantity: 1, price: 10 } ]


const total = arr.reduce((total,item)=>{
   total +=  item.quantity * item.price;
  return total
},0)

1 Comment

not the callback doesn't return anything. The whole reduce will return undefined.

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.