Here you can find the source of flatten()
/******************************************************************************** * Eloquent JavaScript, Ch. 5 - Flattening: * * Use the reduce method in combination with the concat method to ?flatten? an * array of arrays into a single array that has all the elements of the input * arrays.//from ww w.j av a 2s. c om ********************************************************************************/ var arrays = [[1, 2, 3], [4, 5], [6]]; // simple use case: console.log(arrays.reduce(function(a, b){ return a.concat(b); })); // ? [1, 2, 3, 4, 5, 6] // When called on an array of any depth, returns a *new*, completely flat array; // // This is accomplished by progressively 'reducing' the elements in the array. // Given a value 'a', where 'a' contains all previously reduced elements, and // a value 'b', which holds the next element to be reduced: // // 1) if either 'a' or 'b' are arrays, flatten them; // // 2) if 'a' is not an array, convert it to an array placing its original value // inside; // // 3) concatenate 'b' to 'a', and proceed reduction with the result, and next // element in the array; Array.prototype.flatten = function(){ return this.reduce(function(a, b){ if(!Array.prototype.isPrototypeOf(a)) a = [a]; else a = a.flatten(); if(Array.prototype.isPrototypeOf(b)) b = b.flatten(); return a.concat(b); }); }; // using flatten method of Array prototype: arrays.push([[7, 8, 9], [10, [11, [12], null, undefined, NaN, Infinity]]]); console.log(arrays.flatten()); // ? [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, null, undefined, NaN, Infinity] // test for 'arrays' integrity: console.log(arrays);
Array.prototype.flatten = function () { var toReturn = []; for (var i = 0, len = this.length; i < len; i++) { if (this[i] instanceof Array) { toReturn = toReturn.concat(this[i].flatten()); } else { toReturn.push(this[i]); return toReturn; };
Array.prototype.flatten = function() { return this.reduce(function(a,b) { return a.concat(b) }, [])
Array.prototype.flatten = function(){ var flattenArray = []; for (var el in this) { var currElement = this[el]; if (Object.prototype.toString.call(currElement) === '[object Array]' ) { for (var i in currElement) { flattenArray.push(currElement[i]); } else { ...
var slice = Array.prototype.slice; Array.prototype.flatten = function () { var obj, result = []; for (var i = 0, l = this.length; i < l; ++i ) { obj = this[ i ]; if ( Array.isArray( obj ) ) { result = result.concat( obj.flatten() ); } else { result[ result.length ] = obj; ...
Array.prototype.flatten = Array.prototype.flatten || function() { 'use strict'; var that = this; for (var i = 0; i < that.length; i += 1) { if (Array.isArray(that[i])) { that = that.concat.apply([], that); i -= 1; return that; }; Array.prototype.flatten1 = Array.prototype.flatten1 || function() { 'use strict'; var res = []; (function flatten(arr) { for (var i = 0; i < arr.length; i += 1) { if (Array.isArray(arr[i])) { flatten(arr[i]); } else { res.push(arr[i]); }(this)); return res; }; Array.prototype.flatten2 = Array.prototype.flatten2 || function() { 'use strict'; for (var i = 0; i < this.length; i += 1) { if (Array.isArray(this[i])) { this.splice.apply(this, [i, 1].concat(this[i].flatten())); return this; }; var a = [1, 2, [3, 4, [5, 6]]]; console.log(a.flatten()); console.log(a); console.log(a.flatten1()); console.log(a); console.log(a.flatten2()); console.log(a);
Array.prototype.flatten = function(array) { return this.reduce((prev, next) => prev.concat(next), []); };
Array.prototype.flatten = function (func) { var result = []; forEach(this, function (element) { forEach(func(element), function (child) { result.push(child); }); }); return result;
Array.prototype.flatten = function(nil){ result = new Array(); for(var i = 0; i < this.length; i++){ item = this[i]; if($.isArray(item)) for(var j = 0; j < item.length; j++) result.push(item[j]); else result.push(item); return result; }; ...
Array.prototype.flatten = function(prevous) { var flattenedArr = prevous || []; for (var i = 0; i < this.length; i++) { if(Array.isArray(this[i])) { this[i].flatten(flattenedArr); } else if(this[i] === null) { } else { flattenedArr.push(this[i]); return flattenedArr;