Here you can find the source of flatten()
Array.prototype.flatten = function () { return this.reduce(function (flat, toFlatten) { return flat.concat(Array.isArray(toFlatten) ? toFlatten.flatten() : toFlatten); }, []);/*from ww w. j a v a 2s . c om*/ } var array = [1, 2, 3, 4]; console.log(array.flatten()); var array = [1, 2, [3, 4], [5, 6]]; console.log(array.flatten()); console.log(array); // Not changed var array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten());
'use strict'; 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.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); };
Array.prototype.flatten = function() var flat = new Array(); for (var i = 0; i < this.length; i++) { for (var j = 0; j < this[i].length; j++) { flat.push(this[i][j]); return flat; ...
Array.prototype.flatten = function () { return this.map((a) => (a.constructor.name == 'Array' && a.length == 1) ? a[0] : a)
Array.prototype.flatten = function () { var flattenedArray = []; function copyArray(arr) { for (var i = 0; i < arr.length; i += 1) { if (arr[i] instanceof Array) { copyArray(arr[i]); } else { flattenedArray.push(arr[i]); copyArray(this); return flattenedArray; }; var arr1 = [1, 2, [3, 4], 5]; console.log('Original:'); console.log(arr1); console.log(); console.log('Flattened:'); console.log(arr1.flatten());
var outputArray = []; Array.prototype.flatten = function (){ function getValues(array){ var i; for (i = 0; i < array.length; i += 1){ var value = array[i]; if (Array.isArray(value)){ getValues(value); else{ outputArray.push(value); getValues(this); return outputArray; var array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten());