List of utility methods to do Array Flatten
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()); | |
flatten()function flatten (ary) { var ret = [] for (var i = 0; i < ary.length; i++) { if (Array.isArray(ary[i])) { ret = ret.concat(flatten(ary[i])) } else { ret.push(ary[i]) return ret 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 flattenReduce (ary) { return ary.reduce(function (a, b) { if (Array.isArray(b)) { return a.concat(flattenReduce(b)) return a.concat(b) }, []) | |
flatten()Array.prototype.flatten = function (){ var array = this; var newArray = []; recursive(array); function recursive (arr){ for (var i = 0; i < arr.length; i++) { if (arr[i] instanceof Array) { recursive(arr[i]); } else { ... | |
flatten()Array.prototype.flatten = function(){ var array = this; var processedArray = []; for(var i = 0; i < array.length; i++){ if(array[i] instanceof Array){ var subArray = array[i].flatten(); processedArray.push.apply(processedArray,subArray); else { ... | |
flatten()Array.prototype.flatten = function() { function getArrayElements(array, result) { if(!result) { result = []; for (var i=0; i<array.length; i++) { if(Array.isArray(array[i])) { getArrayElements(array[i], result); }else { ... | |
flatten()Array.prototype.flatten = function() { return this.inject([], function(array, value) { if (DiaFluxUtils.isArray(value)) return array.concat(value.flatten()); array.push(value); return array; }); | |
flatten()Array.prototype.flatten = function(){ return this.reduce(function(flat, val, i, hilly){ Array.isArray(val) ? flat.concat(val.flatten()) : flat.concat(val); }, []); | |
flatten()Array.prototype.flatten = function () { var result = []; function flatten(elements, output) { elements.forEach(function (element) { if (!(typeof element === 'object' || typeof element === typeof Array)) { output.push(element); else { flatten(element, output); ... | |
flatten()Array.prototype.flatten = function() { return Array.prototype.concat.apply([], this); | |
flatten()Array.prototype.flatten = function() { var resultArray = []; function getValues(array) { var i; for (i = 0; i < array.length; i += 1) { var value = array[i]; if (Array.isArray(value)) { getValues(value); } else { ... |