New:Introducing Socket Scanning for VS Code Marketplace Extensions.Learn more →
Get Started

asarray

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

asarray

convert array-like objects to arrays

latest
npmnpm
Version
0.1.0
Version published
Weekly downloads
2K
-11.35%
Maintainers
1
Weekly downloads
 
Created
Source

asarray

Convert array-like objects to arrays

Build Status

Have an array-like object, with a length property and integer properties 0 to length - 1, but you want to use real array methods on it? Now you can:

var asarray = require('asarray');

var arr = asarray(obj);

and then Array.isArray(arr) is true, native methods like arr.forEach() (if available on your platform) can be used, and so on. Effectively, asarray converts from an implicit interface to a real array (reification?).

An example use case is iterating over a FileList from the W3C File API or other array-like DOM elements using forEach or other useful array methods. Since FileList is not an array these methods cannot normally be used directly.

Performance note: this module uses new Array(length) then sets each index (initializing the array with [] then adding each element with .push() would be 20-70% slower). Also note, a new array is created and copied; for large array-like objects this module may not be the best solution.

(Note this module should not to be confused with toarray, which although also very useful, has a completely different purpose.)

License

MIT

Keywords

array

FAQs

Package last updated on 27 Apr 2014

Related posts