init-array

1.2.0 • Public • Published

init-array

NPM version

Creates new array with defined size and initial value

Install

npm install --save init-array

Usage

Arguments:

  • dimensions
  • value to assign
var newArray = require('init-array');
 
newArray(3);            // [ 0, 0, 0 ]
newArray(1, 256);       // [ 256 ]
newArray([3, 2]);       // [ [ 0, 0 ], [ 0, 0 ], [ 0, 0 ] ]
newArray([2], 'test');  // [ 'test', 'test' ]
newArray([2, 3], 1);    // [ [ 1, 1, 1 ], [ 1, 1, 1 ] ]
// etc

If value argument is function then executes this function for each element.

newArray(3, Math.random);   // [ 0.5508887629402555, 0.7256682864214947, 0.8916228685296357 ]
 
var i = 0;
initArray([2,2], () => i++) // [ [ 0, 1 ], [ 2, 3 ] ]

Testing

npm test

Package Sidebar

Install

npm i init-array

Weekly Downloads

6

Version

1.2.0

License

MIT

Unpacked Size

5.25 kB

Total Files

5

Last publish

Collaborators

  • saveryanov