0

I have a very simple problem. I wanted to get the total gross using reduce function. My problem is that its a string but I already added + to make it a number. I wonder that output is still NaN?

const hello = [
    {
        "gross": "391.50",
    },
    {
        "gross": "489.80",
    }
]

const final = hello.reduce((prev, cur) => Number(prev.gross) + Number(cur.gross), 0);

console.log(final, 'FINAL')
Joseph
  • 7,042
  • 23
  • 83
  • 181

1 Answers1

0

prev is the running total, not an object, there's no prev.gross.

const hello = [{
    "gross": "391.50",
  },
  {
    "gross": "489.80",
  }
]

const final = hello.reduce((prev, cur) => prev + Number(cur.gross), 0);

console.log(final, 'FINAL')
Barmar
  • 741,623
  • 53
  • 500
  • 612