admin管理员组

文章数量:1429844

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
Add a ment  | 

3 Answers 3

Reset to default 5

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

本文标签: javascriptReduce method os array of objects with a condition is a numberStack Overflow