11 ms·
You can easily extend NodeList.prototype with Array.prototype functions you like, and then just use them: ['map', 'find'].forEach(f => NodeList.prototype[f
by megous 6y ago
You can easily extend NodeList.prototype with Array.prototype functions you like, and then just use them:
['map', 'find'].forEach(f => NodeList.prototype[f] = Array.prototype[f]);
document.querySelectorAll('bla').map(el => whatever)
That's what prototypes are for, anyway.
- rofrol 6y agoNice, ['map', 'find', 'filter', 'reduce'].forEach(f => NodeList.prototype[f] = Array.prototype[f]) // this is for for..of, cannot be above 'map', 'filter' etc. NodeList.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator] document.querySelectorAll('span').map(el => console.log(el)) document.querySelectorAll('span').filter(el => el.className === 'fc-black-500').reduce((acc, el) => (acc += el.className, acc), '') for(let p of document.querySelectorAll('p')) { console.log(p) } - https://stackoverflow.com/questions/30836289/for-of-loop-queryselectorall https://stackoverflow.com/questions/30836289/for-of-loop-que...
- megous 6y agoYou can also create a getter on NodeList.prototype named '_' that returns Array.from(this) and then just use any of the array methods on it. Like document.querySelectorAll('div')._.some(...) So many options. :)