Socket
Socket
Sign inDemoInstall

asarray

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    asarray

convert array-like objects to arrays


Version published
Weekly downloads
361
increased by113.61%
Maintainers
1
Install size
3.62 kB
Created
Weekly downloads
 

Readme

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

FAQs

Last updated on 27 Apr 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc