array.prototype.flat
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
call-bind | 1.0.7 | 21.6 kB | MIT | prod | |
define-properties | 1.2.1 | 5.08 kB | MIT | prod | |
es-abstract | 1.23.3 | 1 B | MIT | prod | |
es-shim-unscopables | 1.0.2 | 4.31 kB | MIT | prod |
Array.prototype.flat is a polyfill, replacement, or shim for the Array.prototype.flat
functionality that is compliant with the ES2019 specification. This JavaScript utility provides a means to flatten multi-dimensional arrays in an environment that supports ES3. This is critically useful in mitigating instances where the native Array.prototype.flat
method may not be available or supported.
To utilize the array.prototype.flat, you first need to install it in your project using npm, the node package manager. Once installed, you can import the function and use it to flatten arrays. The code that demonstrates this is:
var flat = require('array.prototype.flat');
var assert = require('assert');
var arr = [1, [2], [], 3, [[4]]];
assert.deepEqual(flat(arr, 1), [1, 2, 3, [4]]); //array flattened up to 1 level
Moreover, in an instance where Array#flat is not present, you can use the following code:
var flat = require('array.prototype.flat');
var assert = require('assert');
/* when Array#flat is not present */
delete Array.prototype.flat;
var shimmedFlat = flat.shim();
assert.equal(shimmedFlat, flat.getPolyfill());
Finally, when Array#flat is present, the following code can be used:
var flat = require('array.prototype.flat');
var assert = require('assert');
/* when Array#flat is present */
var shimmedIncludes = flat.shim();
var mapper = function (x) { return [x, 1]; };
assert.equal(shimmedIncludes, Array.prototype.flat);
assert.deepEqual(arr.flat(mapper), flat(arr, mapper));
The comprehensive documentation and information about array.prototype.flat can be found in the README file of the package on GitHub. The URL is git://github.com/es-shims/Array.prototype.flat.git. One can also refer to the es-shim API interface for additional understanding on how to use the package. There, you will find all the necessary instructions on using and understanding this library, sample usage patterns, installation instructions, and information regarding the test suite.