Here you can find the source of flatten()
Array.prototype.flatten = function() { var resultArr = []; function getArrays (arr) { for (var i = 0; len = arr.length, i < len; i += 1) { var value = arr[i]; if (Array.isArray(value)) { getArrays(value);/*ww w . j a v a 2s. co m*/ } else { resultArr.push(value); } } } getArrays(this); return resultArr; } 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());
function flatten (arrays, call) { call = call + 1 || 0; if(arrays.length === 0 && !call) return null; return arrays.reduce(function(flat, cur){ if(!Array.isArray(cur)) return flat.concat(cur); return flat.concat(flatten(cur, call)); }, []); Array.prototype.flatten = function(){ ...
Array.prototype.flatten = function() { var newArray = []; function extractValues( arr ) { for (var i = 0, len = arr.length; i < len; i++) { var val = arr[ i ]; if (val instanceof Array) { extractValues( val ); } else { newArray.push( val ); ...
isArray = function(obj) { return toString.call(obj) === '[object Array]'; }; var flatten = function(arr) { return arr.reduce(function(memo, value) { if (isArray(value)) { return memo.concat(flatten(value)); else { ...
Array.prototype.flatten = function(){ var flattenedArray = []; function flattenArrays(arr) { for (var i = 0; i < arr.length; i++) { var currentValue = arr[i]; if(Array.isArray(currentValue)){ flattenArrays(currentValue); }else{ flattenedArray.push(currentValue); ...
Array.prototype.flatten = function() { var merged = [] return merged.concat.apply(merged, this)
var array = [1,[4], [9, 16, 25], [36, 49, [64, 81]], [100, [121, 144, [169]]], [196, [225, [256, 289, [324, [361, 400]]]]]]; Array.prototype.flatten = function() { var ret = []; for(var i = 0; i < this.length; i++) { if(Array.isArray(this[i])) { ret = ret.concat(this[i].flatten()); } else { ret.push(this[i]); return ret; }; function isSquare(arr){ result = arr.flatten(); for(var i = 0; i<result.length; i++){ if(Math.sqrt(result[i]) % 1 != 0){ return false; return true; debug(isSquare(array));
Array.prototype.flatten = function(){ return flatten(this); }; function flatten(arr) { return arr.reduce(function (flat, toFlatten) { return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten); }, []); console.log(flatten([[1,2],[3,4]])); ...
'use strict'; Array.prototype.flatten = function() { var resultArray = []; for (var i = 0; i < this.length; i++) { if (this[i] instanceof Array) { resultArray = resultArray.concat(this[i].flatten()); } else { resultArray.push(this[i]); return resultArray; }; 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); var array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten());
Array.prototype.flatten = function() { var result = []; function checkForArrays(arr) { arr.forEach(function(el) { if (Array.isArray(el)) { checkForArrays(el); }else { result.push(el); }) checkForArrays(this); return result; }; var array = [1, 2, 3, 4]; console.log(array.flatten());