For instance, if we have the following HTML:
<p> foo </p> <p> bar </p>
Then we can convert the nodelist with the selected p elements and call
filter on it by writing:
const nodes = document.querySelectorAll('p') const texts = [...nodes].map(n => n.textContent) console.log(texts) const filtered = [...nodes].filter(n => n.textContent.includes('foo')) console.log(filtered)
document.querySelectorAll to return a nodelist with the p elements.
Then we use the spread operator to spread the items in the
nodes nodelist into an array.
And then we call
map to map the node array to an array of text.
Then we call
filter on the array of nodes with a callback to return the nodes with
textContent that includes
[“n foon”, “n barn”]
filtered has an array with the first p element.