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?

2
  • 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 Commented Apr 30, 2018 at 13:00
  • I didn't thought that way, it's really a misuse of the map function, thanks! Commented Apr 30, 2018 at 13:09

1 Answer 1

1

Your code should be

data.map((current,index, array) => {
    const prevVisitors = array[index - 1 ] ? array[index - 1].visitors : 0
    return current.visitors += prevVisitors
})
Sign up to request clarification or add additional context in comments.

1 Comment

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

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.