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.

CodeBreaker
  • 95
  • 4
  • 12
  • 4
    you aren't returning anything. either use `return` or drop the curlies – Daniel A. White May 18 '18 at 01: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))``` – Vipulw Oct 27 '20 at 08:06
  • @Vipulw please post a new question with a [mcve] – Daniel A. White Oct 27 '20 at 14:07

2 Answers2

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.

Praveen Kumar
  • 1,966
  • 1
  • 9
  • 16
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));
Isaac
  • 11,409
  • 5
  • 33
  • 45