[Fixed] TypeError: map is not a function in Javascript

map is not a function in Javascript

TypeError: .map is not a function occurs when we call map() function on object which is not an array. map() function can be only called on array.

Let’s see with help of simple example:

You will get following error when you execute the program:

We got this error because numObj is not an array, it is an object.

To correctly call map() function, we need to use numObj.Numbers. map() function will multiply elements of Numbers array by 2.

Output:

You can avoid the error by using Array.isArray() method to check if value is an array. If it is not an array, then we can return empty value.

Output:

As you can see numObj is not an array, so result is [].

If you have array like objects, then first convert them to array using Array.from() method and then call map method on it.

As you can see, we have converted set to Array using Array.from() method then called .map() function to convert country names to uppercase.

If you are getting this error while dealing with JSON, you need to make sure that you are calling map() function only on arrays. It may be good idea to log the JSON on console and see if you are calling map() on correct value.

map is not a function in react

If you are getting issue in react, you need to first check if calling object is array or not using Array.isArray() method.

If it is not an array, then you may need to convert your object to array and call .map() method on it.

That’s all about how to resolve .map is not a function in Javascript.

Was this post helpful?

Leave a Reply

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