2.0.0 • Published 5 years ago

nodelist-to-array v2.0.0

Weekly downloads
3
License
ISC
Repository
github
Last release
5 years ago

What for

Convert array like object to array, for example to unlock reduce(), map(), filter()...

Fast and very straight forward. Working with everything what have a .length:

Benchmark

https://jsperf.com/nodelist-to-array/27

Code and usage

function nodeListToArray(nodeList){
  var i = nodeList.length
  var arr = new Array(i)
  while(i--){ arr[i] = nodeList[i] }
  return arr
}

// example of usage:
nodeListToArray(document.querySelectorAll('div')).map(...)

Prototype

On top of that fancy addition, but working only with NodeList's:

NodeList.prototype.toArray = function(){
  return nodeListToArray(this)
}

// example of usage:
document.querySelectorAll('div').toArray().filter(...)

Instalation

npm install nodelist-to-array