Here you can find the source of flaten()
'use strict';/*ww w. j a v a2s . c o m*/ // TASK 03 var array = [1, 2, 3, 4]; var array = [1, 2, [3, 4], [5, 6] ]; var array = [0, ["string", "values"], 5.5, [ [1, 2, true], [3, 4, false] ], 10]; Array.prototype.flaten = function() { var flatenArray = []; var extractArray = function(arr) { if (arr.constructor === Array) { for (var i = 0; i < arr.length; i += 1) { extractArray(arr[i]); } } else { flatenArray.push(arr); } return flatenArray; } return extractArray(this); } // TESTS // console.log(array); // ORIGINAL ARRAY IS NOT TOUCHED console.log(array.flaten());
Array.prototype.flat = function() { return this.reduce(function(a, b) { return a.concat(b); }, []); }; var range = function(n) { var ret = []; for (var i = 0; i < n; i++) { ret.push(i); ...
Array.prototype.flatten = function() { return this.reduce(function (a, b) { return a.concat(b instanceof Array ? b.flatten() : b); }, []); };
Array.prototype.flatten = function () { var flattened = Array.prototype.concat.apply([], this); if (flattened.length == this.length) return flattened; else return flattened.flatten();
Array.prototype.flatten = function() { return Array.prototype.concat.apply([], this); };
Array.prototype.flatten = function() { "use strict"; var flattened = []; flattened.concat.apply(flattened, this); };