Array.from()
lets you convert an "iterable" object (AKA an array-like object) to an array. In this lesson, we go over grabbing DOM nodes and turing them into an array so that we can use methods like Array.filter()
and Array.forEach()
on them.
Array.from() example
- 15.99
- 7.99
- 32.99
- 24.99
- 5.99
const products = Array.from(document.querySelectorAll('.product'));products .filter(product => parseFloat(product.innerHTML) < 10) .forEach(product => product.style.color = 'red');
What we got from document,querySelectorAll('.product') is 'NodeList', it is an array-like type, but cannot apply .filter, .map, .forEach to it. SO we use Array.from() method to convert is.