1
return valuesArray.map((objValue) => {
         Dataset.find(dataObj => dataObj.value === objValue)
});

it returns undefined. However, if I use forEach and push values into an new array, it works.

3
  • 4
    you aren't returning anything. either use return or drop the curlies Commented May 18, 2018 at 1:45
  • @CodeBreaker This behavior is strange.Even I ma doing same and I am getting undefined, even I have returned properly. @Daniel A. White can you help here ? resources.map((resource) => members.find((member) => member.identifier === resource)) Commented Oct 27, 2020 at 8:06
  • @Vipulw please post a new question with a minimal reproducible example Commented Oct 27, 2020 at 14:07

2 Answers 2

7

You can also check with filter to check for undefined.

return valuesArray.map((objValue) => {
        return Dataset.find(dataObj => dataObj.value === objValue)
}).filter(y => y != undefined);

So it will not return the undefined from the valuesArray also.

Sign up to request clarification or add additional context in comments.

Comments

1

You're missing a return value.

With anonymous functions, if you encase the function in curly braces, you have to explicitly return a value.

  • () => 1: returns 1
  • () => { 1 }: returns undefined
  • () => ({}): returns {}

To answer your question, here are 2 methods that will work:

return valuesArray.map((objValue) => {
         return Dataset.find(dataObj => dataObj.value === objValue)
});

or

return valuesArray.map((objValue) => Dataset.find(dataObj => dataObj.value === objValue));

1 Comment

Consider adding an explanation as to why the OP's code didn't work and what you've changed

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.