Evans Evans - 5 months ago 26
Javascript Question

Filter is not a function?

Im attempting to return an array of my list items if they contain the text "Flexbox" in them.

Im getting this error:

Uncaught TypeError: items.filter is not a function


<ul>
<li data-time="5:10">Flexbox 1</li>
<li data-time="5:10">Something else</li>
<li data-time="5:40">Flexbox 2</li>
<li data-time="5:10">Something also else</li>
<li data-time="5:40">More Flexbox for you</li>
</ul>


'use strict';

var items = document.querySelectorAll('li');

var itemsFlex = items.filter(function(item) {
return item.includes('Flexbox')
});

console.log(itemsFlex);

Answer Source

querySelectorAll returns a NodeList not an Array. You can convert it to an Array if you'd like to use Array methods.

var items = document.querySelectorAll('li');
var itemsArray = Array.from(items);