Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript .map() is not a function

I am new here (and new to JavaScript), so please excuse my super basic questions. I have a HTML page with different images that all share a class on it. By using getElementsByClassName, I get an array. I want to add an event listener to each of the cells in the array by using the .map() function.

This is what I have:

window.onload = function(){
var allImgs = document.getElementsByClassName("pft");

var newImgs = allImgs.map(
        function(arrayCell){
            return arrayCell.addEventListener("mouseover, functionName");
        }
    );
}; 

This keeps showing the error "allImgs.map is not a function" even when I change the inner function to something that doesn't include the event listener.

I have another version of this code where I just loop through the array cells within window.onload and add the event listener to each of them that way and it works. Why isn't the .map() function working? Can it not be used in window.onload?

like image 426
JSn00b Avatar asked Jul 28 '15 12:07

JSn00b


People also ask

How do I fix map is not a function?

The "TypeError: map is not a function" occurs when we call the map() method on an object that is not an array. To solve the error, console. log the value you're calling the map() method on and make sure to only call the map method on valid arrays.

Is map a function in JavaScript?

map() creates a new array from calling a function for every array element. map() calls a function once for each element in an array. map() does not execute the function for empty elements. map() does not change the original array.

Is map a callback function?

The map() method calls a callback function on every element of an array and returns a new array that contains the results. The map() method takes two named arguments, the first one is required whereas the second one is optional.

What is map function ES6?

ES6 - Array Method map() map() method creates a new array with the results of calling a provided function on every element in this array.


4 Answers

getElementsByClassName() returns an HTMLCollection not an Array. You have to convert it into a JavaScript array first :

allImgs = Array.prototype.slice.call(allImgs);  // or allImgs = [].slice.call(allImgs);  // or (thanks @athari) allImgs = Array.from(allImgs);  // or (thanks @eliaz-bobadilla) allImgs = [...allImgs] 
like image 106
flawyte Avatar answered Sep 20 '22 16:09

flawyte


By using getElementsByClassName, I get an array.

No, you don't.

You get a live HTMLCollection. This is array-like but is not an array.

Since it is sufficiently array-like, you can apply the map method from a real array.

    var text_content = [].map.call(        document.getElementsByClassName("sdf"),        function (currentValue, index, collection) {          return currentValue.innerHTML;        }      );      console.log(text_content);
  <p class="sdf">foo</p>    <p class="sdf"></p>    <p class="sdf">bar</p>    <p class="sdf"></p>    <p class="sdf"></p>            
like image 22
Quentin Avatar answered Sep 20 '22 16:09

Quentin


Another option would be to use map directly:

[].map.call(allImages, function() { ... });

However, what you are doing is better achieved with Array.prototype.forEach.

like image 40
Rustem Mustafin Avatar answered Sep 17 '22 16:09

Rustem Mustafin


Compact syntax:

[...document.getElementsByClassName("pft")].map(arrayCell => arrayCell.addEventListener("mouseover", "functionName"))
like image 37
Ari Singh Avatar answered Sep 18 '22 16:09

Ari Singh