Sometimes, we want to fix JavaScript map function return undefined.
In this article, we’ll look at how to fix JavaScript map function return undefined.
How to fix JavaScript map function return undefined?
To fix JavaScript map function return undefined, we use filter instead of map for filtering.
For instance, we write
const arr = ["a", "b", 1];
const results = arr.filter((item) => {
return typeof item === "string";
});
to call arr.filter with a callback that returns if the item in arr has type 'string'.
If it is, we add it to the returned array.
Otherwise, we skip it.
Conclusion
To fix JavaScript map function return undefined, we use filter instead of map for filtering.