Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array.prototype.flat is undefined in nodejs

Given is the following js command:

[].flat; 

Executing it in a browser (chrome/firefox): returns function

Executing it with nodejs v10.13.0: returns undefined

Now I am wondering what other methods are not in the node RTE and where the documentation of global Objects like Array is.

Seems to be the same with Array.prototype.flatMap.

like image 986
MaximilianMairinger Avatar asked Oct 30 '18 20:10

MaximilianMairinger


People also ask

What is array prototype in JavaScript?

The JavaScript array prototype constructor is used to allow to add new methods and properties to the Array() object. If the method is constructed, then it will available for every array. When constructing a property, All arrays will be given the property, and its value, as default. Syntax: Array.prototype.name = value.

What is flat and flatMap in JavaScript?

The flat() method creates a new array with the elements of the subarrays concatenated into it. The map() method creates a new array whose elements are the results of a mapping function. The flatMap() method is the combination of the map() method followed by the flat() method of depth 1.


Video Answer


2 Answers

You'll need node 11 or higher for Array.prototype.flat. MDN is a great resource both for learning about Javascript and as a reference. You will find information about usage, browser compatibility, and even polyfills there.

As for Node.js support of ES6+, the best resource is node.green with a very detailed and up to date list of supported features by node version.

like image 90
lucascaro Avatar answered Oct 04 '22 12:10

lucascaro


It doesn't seem to be supported in node.js at the moment. A quick ES6 way of flattening (depth 1) an array:

[[1, 2], [3, 4]].reduce((acc, val) => [ ...acc, ...val ], []) 
like image 21
johng Avatar answered Oct 04 '22 11:10

johng