0

In this case, I'm making a sum of the object and its working just as I wanted to, the problem is the following message:

Expected to return a value in arrow function array-callback-return

Here is the code:

data.map((current,index, array) => {
    const prevVisitors = array[index - 1 ] ? array[index - 1].visitors : 0
    current.visitors += prevVisitors
})

What should I use as a return statement if there is nothing to be returned(Since it's only a modification of the props)? And in what cases this error is useful?

Paliao
  • 71
  • 3
  • 11
  • 4
    `map` works that you need to return the changed value so you can `return current` after you changed it, even if in these cases a `forEach` could be more suitable – quirimmo Apr 30 '18 at 13:00
  • I didn't thought that way, it's really a misuse of the map function, thanks! – Paliao Apr 30 '18 at 13:09

1 Answers1

1

Your code should be

data.map((current,index, array) => {
    const prevVisitors = array[index - 1 ] ? array[index - 1].visitors : 0
    return current.visitors += prevVisitors
})
Laura delgado
  • 362
  • 2
  • 8
  • 21
  • Yes, I used this at first, but with the answer of @quirimmo I thought better, and use forEach it's a better practice than return the current value, thanks – Paliao May 01 '18 at 02:02