Skip to content
Advertisement

Array.prototype.map() expects a value to be returned, but can’t return null as per other questions, at the end of arrow function

I am using the map function with an if statement that doesn’t end in else. I have to be strict that only the defined recourseTypes are shown.

in eslint I get an error on my => as it doesn’t end in an else

Array.prototype.map() expects a value to be returned at the end of arrow function.

Is there a correct way to write this?

Code:

JavaScript

Advertisement

Answer

Clarification : resources array contains all the resourceType that we are comparing using if/else clause ? As Array.map() will return the same number of elements as input array contains otherwise it will return with undefined.

Suggestion : Instead of multiple if/else-if clause, we can use multiple if statements for a better performance.

Demo :

JavaScript

Performance test result screenshot :

enter image description here

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement