I have an array:
const products = [
{ product: 'banana', price: 3 },
{ product: 'mango', price: 6 },
{ product: 'potato', price: ' ' },
{ product: 'avocado', price: 8 },
{ product: 'coffee', price: 10 },
{ product: 'tea', price: '' },
]
I have an array:
const products = [
{ product: 'banana', price: 3 },
{ product: 'mango', price: 6 },
{ product: 'potato', price: ' ' },
{ product: 'avocado', price: 8 },
{ product: 'coffee', price: 10 },
{ product: 'tea', price: '' },
]
And Iwould like to sum all prices. What I tried:
const sum = products.reduce(function(acc, cur){
if (Number.isInteger(cur.price))
return acc+cur.price
}, 0)
console.log(sum)
it returns undefined. I also tried that without the condition, it returns a string. Where do I make a mistake?
Share Improve this question asked Oct 18, 2020 at 16:21 Al_MilliganAl_Milligan 1191 gold badge2 silver badges12 bronze badges 3-
1
You forgot to declare
products
in the second snippet. Also, the first snippet is unnecessary; just use a code block if your code doesn't have any output. – ElectricShadow Commented Oct 18, 2020 at 16:25 -
You need a return for
else
also – charlietfl Commented Oct 18, 2020 at 16:26 - Return the accumulator after the if-condition – Kunal Mukherjee Commented Oct 18, 2020 at 16:29
3 Answers
Reset to default 5You need to return the accumulator if the condition is not true
.
const
products = [{ product: 'banana', price: 3 }, { product: 'mango', price: 6 }, { product: 'potato', price: ' ' }, { product: 'avocado', price: 8 }, { product: 'coffee', price: 10 }, { product: 'tea', price: '' }],
sum = products.reduce(function(acc, cur) {
if (Number.isInteger(cur.price)) return acc + cur.price;
else return acc;
}, 0);
console.log(sum);
A shorter approach adds the value conditionally and returns only the accumulator at the end.
const
products = [{ product: 'banana', price: 3 }, { product: 'mango', price: 6 }, { product: 'potato', price: ' ' }, { product: 'avocado', price: 8 }, { product: 'coffee', price: 10 }, { product: 'tea', price: '' }],
sum = products.reduce(function(acc, cur) {
if (Number.isInteger(cur.price)) acc += cur.price;
return acc;
}, 0);
console.log(sum);
Try this:
const products = [
{ product: 'banana', price: 3 },
{ product: 'mango', price: 6 },
{ product: 'potato', price: ' ' },
{ product: 'avocado', price: 8 },
{ product: 'coffee', price: 10 },
{ product: 'tea', price: '' },
]
const sum = products.filter(x => typeof x.price === 'number').map(x => x.price).reduce((a, b) => a + b);
console.log(sum)
The result of your accumulator for the last element tea is undefined
because you don't return anything, if price
is not a number. And as this is the last call to the accumulator, also the result of reduce
is undefined
.
If you just want to ignore values with invalid price, you can either use
products.filter(x => Number.isInteger(x.price)).reduce (...)
to only sum up products with a valid price
or return for instance acc
if cur.price
is not a number.
const sum = products.reduce(function(acc, cur){
if (Number.isInteger(cur.price))
return acc+cur.price;
return acc;
}, 0)
console.log(sum)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745430168a4627355.html
评论列表(0条)