Categories
JavaScript Answers

How to fix JavaScript map function return undefined?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *