0
How can I put a condition if in my function for it not to return a value undefined?
This is my job:
keys.map(key => ({ key, value: data[key] }))
She returns this:
[ { key: 'a', value: 2 },
{ key: 'b', value: 2 },
{ key: 'c', value: 2 },
{ key: 'd', value: undefined },
{ key: 'e', value: undefined },
{ key: 'f', value: 2 } ]
I tried in some ways but without success.
One of my attempts:
keys.map(key => (if(data[key]!=undefined) {{key, value: data[key]}}))
If you just want to eliminate the values
undefinedyou don’t need amap, but of afilter. Themapsets a 1:1 relation and always returns an output for each input.– Woss
To address issues related to Arrays and Objects I strongly recommend you to use https://underscorejs.org - a Javascript library that provides a range of auxiliary programming functions . For example, in this case just do this:
_.without(keys, undefined)and returns a copy of the array without the Undefined– Cristiano Gilberto João