Hacker News new | past | comments | ask | show | jobs | submit login

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.



Nice,

  ['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-que...


You 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. :)




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: