Javascript Array flatten()
Array.prototype.flatten = function() { return this.reduce(function(a, b) { return a.concat(b); }, []);/*from w w w .j a v a 2 s. c o m*/ };
Array.prototype.$flatten = function() { return this.reduce(function(a, b) { return a.concat(b); }, []);//from w w w . ja va 2 s . c o m };
// Returns a single Array of elements from a given array of arrays // Example: [[0,1],[2,3],[4,5]].flatten() -> [0,1,2,3,4,5] Array.prototype.flatten = function() { return Array.prototype.concat.apply([], this); };
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 this.reduce(function(a, b) { return a.concat(b); });/*from w w w . j a v a2 s .c o m*/ };
Array.prototype.flatten = function() { let result = []; const execute = (arr) =>{// w w w . j a v a2s.co m for(let i=0;i<arr.length;i++){ let element = arr[i]; Array.isArray(element)?execute(element):result.push(element); } } execute(this); return result; } let arr = [1,4,23,99,[32,false],[[14,2],[[["last element"]]]]]; console.log(arr.flatten());
Array.prototype.flatten = function() { "use strict"; var flattened = []; flattened.concat.apply(flattened, this); };
Array.prototype.flatten = function() { return [].concat.apply([], this); };
Array.prototype.flatten = function(){ var i, iterations, output; output = [];//from ww w. ja v a 2s . com flatten(this); return output; function flatten(array){ iterations = array.length; for (i = 0; i < iterations; i+=1){ if (array[i] instanceof Array){ flatten(array[i]); }else{ output.push(array[i]); } } } };
function flatten() { var result = []; for (var i = 0; i < this.length; i++) { if (this[i] instanceof Array) { var flattened = this[i].flatten(); for (var j = 0; j < flattened.length; j++) { result.push(flattened[j]); }/* w ww . ja v a 2 s . c o m*/ } else { result.push(this[i]); } } return result; } Array.prototype.flatten = flatten; var arr = [1, [2, 5, [6, 7, "str"], []], 3]; console.log(arr.flatten());
Array.prototype.flatten = function flatten() { var output = []; return output.concat.apply(output, this); };
Array.prototype.flatten = function (){ var result = []; function recursiveArrTraverse(arr){ for (var index in arr) { if (Array.isArray(arr[index])){ recursiveArrTraverse(arr[index]); }else {//from w ww. j a v a2 s . c o m result.push(arr[index]); } } result.splice(result.length-1, 1); } recursiveArrTraverse(this); return result; }; var array = [1, 2, [3, 4], [5, 6]]; console.log(array.flatten());
Array.prototype.flatten = function flatten(){ var flat = []; for (var i = 0, l = this.length; i < l; i++){ var type = Object.prototype.toString.call(this[i]).split(' ').pop().split(']').shift().toLowerCase(); if (type) { flat = flat.concat(/^(array|collection|arguments|object)$/.test(type) ? flatten.call(this[i]) : this[i]); } }/*from w w w. j a v a 2s .c om*/ return flat; };
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]);// www . j a v a 2 s. c o m } } return flat; }
Array.prototype.flatten = function () { return this.reduce(function (flat, toFlatten) { return flat.concat(Array.isArray(toFlatten) ? toFlatten.flatten() : toFlatten); }, []);//from w ww. java 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); var array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten());
Array.prototype.flatten = function () { return this.reduce(function (flat, toFlatten) { return flat.concat(Array.isArray(toFlatten) ? toFlatten.flatten() : toFlatten); }, []);//from w w w. j a va 2 s.c o m } 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());
Array.prototype.flatten = function flatten() { var flattenedArray = []; for (var i = 0; i < this.length; i++) { if (this[i] instanceof Array) { flattenedArray = flattenedArray.concat(this[i].flatten()); } else {//from w ww . j av a 2 s. co m flattenedArray.push(this[i]); } } return flattenedArray; }; 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());
Array.prototype.flatten = function() { var newArray = []; // w w w . j a v a2 s .c om 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 ); } } } extractValues( this ); return newArray; } var array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten());
'use strict';/*from w ww. j a va 2 s. co m*/ 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); // Not changed var array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.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);/*w ww .j ava 2 s . c o 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());
var outputArray = []; Array.prototype.flatten = function (){ function getValues(array){ var i;/* w ww .ja va2 s . c om*/ 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());
// http://www.freecodecamp.com/challenges/bonfire-steamroller // converts a multi-dimentional array into a single flat array by using recursion Array.prototype.flatten = function() { var newArr = []; this.map(function(value) { if (Array.isArray(value)) { newArr = newArr.concat(value.flatten()); } else {/*from www . j ava 2s. c o m*/ newArr.push(value); } }); return newArr; }; function steamroller(arr) { // I'm a steamroller, baby var flat = arr.flatten(); console.log('flat', flat); return flat; } steamroller([1, [2], [3, [[4]]]]);
Array.prototype.flatten = function() { var arr = []; function flattenArr(array) { var i = 0, length = array.length; for (i; i < length; i += 1) { var el = array[i]; if (Array.isArray(el)) { flattenArr(el);/*w ww . jav a2 s. c o m*/ } else { arr.push(el); } } } flattenArr(this); return arr; }; var array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten());
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 {//ww w.j a v a 2 s .c om 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(){ var flattenedArray = []; function flattenArrays(arr) { for (var i = 0; i < arr.length; i++) { var currentValue = arr[i]; if(Array.isArray(currentValue)){ flattenArrays(currentValue); }else{// w w w. j a v a 2 s.c o m flattenedArray.push(currentValue); } } } flattenArrays(this); return flattenedArray; }; var array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten()); console.log("\n\n");
Array.prototype.flatten = function () { var resultArray = []; function getValues(array) { for (var i = 0; i < array.length; i++) { if (Array.isArray(array[i])) { getValues(array[i]);//from w ww . ja v a 2 s . co m } else { resultArray.push(array[i]); } } } getValues(this); return resultArray }; var array = [1, 2, [3, 4], [5, 6]]; var flattenedArray = array.flatten(); console.log(flattenedArray);
'use strict';/*from w w w.j av a 2 s .c o m*/ Array.prototype.flatten = function(){ var newArr = [], _this = this; function checkArrElem(arr){ var i, len = arr.length; for(i = 0; i < len; i++){ if(Array.isArray(arr[i])){ checkArrElem(arr[i]); }else{ newArr.push(arr[i]); } } } checkArrElem(_this); return newArr; } var array = [1, 2, 3, 4]; console.log(array.flatten()); console.log('\r\n'); var array = [1, 2, [3, 4], [5, 6]]; console.log(array.flatten()); console.log(array); // Not changed console.log('\r\n'); var array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten());
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]);/*from w w w . ja v a2 s .c om*/ } 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());
Array.prototype.flatten = function () { "use strict"; var result = []; function flatt(arr) { arr.forEach(function (e) { if (Array.isArray(e)) { flatt(e);// www.j av a2 s. com } else { result.push(e); } }); } flatt(this); return result; }; //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());
Array.prototype.flatten = function() { flattenArrayOfArrays = (array, result) => { if (!result) { result = [];/*from www .j av a2 s . c o m*/ } for (var i = 0; i < array.length; i++) { if (Array.isArray(array[i])) { flattenArrayOfArrays(array[i], result); } else { result.push(array[i]); } } return result; } return flattenArrayOfArrays(this); }; // // Examples // const array = [1, 2, ["abc", "aaa", "bbb"], 4, 4.5, [[true, 1, 2], [3, false, 4]], 10]; console.log(array); console.log(array.flatten());
Array.prototype.flatten = function() { var result = []; function checkForArrays(arr) { arr.forEach(function(el) { if (Array.isArray(el)) { checkForArrays(el);/*from ww w .j a v a 2s . c o m*/ }else { result.push(el); } }) } checkForArrays(this); return result; }; 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());
Array.prototype.flatten = function() { var merged = [] return merged.concat.apply(merged, this) }
function flatten (ary) { var ret = []/*w w w . j a va 2 s .co m*/ 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) }, []) }
Array.prototype.flatten = function flatten() { var outputArray = []; appendChilds(this);//w w w.j av a2s . c om return outputArray; function appendChilds(array) { var count = array.length; for(var i = 0; i < count; i += 1) { if(array[i] instanceof Array) { appendChilds(array[i]); } else { outputArray.push(array[i]); } } } } var array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten());
// Array.prototype.flatten = function(){ // return this.reduce(function (flat, toFlatten) { // return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten); // }, []);//from w w w .ja v a 2 s.com // }; 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]])); console.log(flatten([[1,2],3,4])); console.log(flatten([1, [2], [3, [[4]]]])); console.log([1, [2], [3, [[4]]]].flatten());
/*/* w w w . j a v a2s .c o m*/ Given an array of arrays, return all elements in a single array. You must use recursion. */ 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(){ return this.reduce(function(flat, val, i, hilly){ Array.isArray(val) ? flat.concat(val.flatten()) : flat.concat(val); }, []); }
Array.prototype.flatten = function () { return this.reduce((memo, arr) => memo.concat(arr), []) }
Array.prototype.flatten = function flatten() { var newArr = []; var array = this; function innerFlatten(arr){ for (var i = 0; i < arr.length; i++) { if(arr[i] instanceof Array ){ innerFlatten(arr[i]);/* ww w . j a v a2s.co m*/ } else{ newArr.push(arr[i]); } } } innerFlatten(array); return newArr; } var array = [1, 2, 3, 4]; console.log(array.flatten()); var array = [1, 2, [[7,8], 4], [5, 6]]; console.log(array.flatten()); console.log(array);
Array.prototype.flatten = function flatten() { var result = []; function recursiveArrTraverse(arr) { for (var index in arr) { if (Array.isArray(arr[index])) { recursiveArrTraverse(arr[index]); } else {// w ww. j a va 2 s. c o m result.push(arr[index]); } } result.splice(result.length - 1, 1); } recursiveArrTraverse(this); return result; }; var array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten());
/**/* w ww. java 2s. co m*/ * Created by ufree on 2/18/2016. */ 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 { processedArray[i] = array[i] } } return processedArray; }; var array = [1, 2, [3, 4], [5, 6]]; console.log(array.flatten()); console.log(array); // Not changed array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten());
Array.prototype.flatten = function(){ var arr = []; function validateArray(array) { if (!Array.isArray(array)) { return true; } //from w w w.j a v a 2 s. co m } for (var i = 0; i < this.length; i++) { if(validateArray.call(this[i])){ arr.push(this[i]); }; for (var j = 0; j < this[i].length; j++) { if(validateArray.call(this[i][j])){ arr.push(this[i][j]); } else{ flatten.call(this[i][j]); } }; }; return arr; } 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 array = this; var newArray = []; recursive(array);/*from w ww.j a va 2 s.co m*/ function recursive (arr){ for (var i = 0; i < arr.length; i++) { if (arr[i] instanceof Array) { recursive(arr[i]); } else { newArray.push(arr[i]); }; } } return newArray; } console.log("First sample code"); var array = [1, 2, 3, 4]; console.log(array.flatten()); console.log(); console.log("Second sample code"); var array = [1, 2, [3, 4], [5, 6]]; console.log(array.flatten()); console.log(array); // Not changed console.log();
Array.prototype.flatten = function flatten(){ return [].concat.apply([], this); };
Array.prototype.flatten = function() { return this.reduce((a,b) => {return a.concat(b);}, []); }; let assert = require('assert'); describe('Array', () => { describe('#indexOf', () => { const subject = [1,2,3];/*from w w w .ja v a2s .c o m*/ it('should return -1 if value is not present', () => { assert.equal(-1, subject.indexOf(4)); }); it('should return the position if value is present', () => { assert.equal(2, subject.indexOf(3)); }); }); describe('#flatten', () => { const subject = [[1,2], [], [3,4,5], [6]]; it('should flatten the array', () => { assert.deepEqual([1,2,3,4,5,6], subject.flatten()); }); }); });
/**//w w w. j a v a 2 s. c o m * Created by Edi on 14-Feb-16. */ 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 { resultArray.push(value); } } } getValues(this); 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); // Not changed var array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten());
"use strict";/*from ww w . j av a 2 s .com*/ Array.prototype.flatten = function () { var result = []; function getValues(array) { var i; for (i = 0; i < array.length; i++) { var value = array[i]; if (Array.isArray(value)) { getValues(value); } else { result.push(value); } } } getValues(this); return result; }; var array = [1, 2, 3, 4]; console.log(array.flatten()); array = [1, 2, [3, 4], [5, 6]]; console.log(array.flatten()); console.log(array); // Not changed array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.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);// www .j a v a 2 s . co m } else { flatten(element, output); } }); } flatten(this, result); return result; }; var array = [1, 2, 3, 4]; console.log(array.flatten()); array = [1, 2, [3, 4], [5, 6]]; console.log(array.flatten()); console.log(array); array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten());
"use strict";/*from w ww. jav a2s . c om*/ Array.prototype.flatten = function () { var arr = [], i; function getElements (array) { for (i in array) { if (Array.isArray(array[i])) { getElements(array[i]); } else if (typeof array[i] != "function") { arr.push(array[i]); } } } getElements(this); return arr; }; 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()); console.log(array);
Array.prototype.flatten = function() { var flatten = function(items) { var flattened = []; items.forEach(function(item) { if (typeof item === "object") { flatten(item).forEach(function (i) { flattened.push(i);//from w ww .j a v a2 s. c o m }); } else { flattened.push(item); } }); return flattened; } return flatten(this); }; var array = [1, 2, 3, 4]; console.log(array.flatten()); array = [1, 2, [3, 4], [5, 6]]; console.log(array.flatten()); console.log(array); // Not changed [1, 2, 3, 4, 5, 6] array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten());
Array.prototype.flatten = function() { var resultArray = []; function getValues(array) { var i;/*from www . ja va 2 s. c o m*/ for (i = 0; i < array.length; i += 1) { var value = array[i]; if (Array.isArray(value)) { getValues(value); } else { resultArray.push(value); } } } getValues(this); 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); // Not changed var array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten());
Array.prototype.flatten = function(){ return this.reduce(function(flat, val, i, hilly){ Array.isArray(val) ? flat.concat(val.flatten()) : flat.concat(val); }, []);/* w w w . j a va 2 s . c om*/ }
Array.prototype.flatten = function() { function getArrayElements(array, result) { if(!result) { result = [];/*from w w w.jav a 2 s. c om*/ } for (var i=0; i<array.length; i++) { if(Array.isArray(array[i])) { getArrayElements(array[i], result); }else { result.push(array[i]); } } return result; } return getArrayElements(this); } var array = [1, 2, 3, 4]; console.log(array.flatten()); var array = [1, 2, [3, 4], [5, 6]]; console.log(array.flatten()); var array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten());
Array.prototype.flatten = function () { return this.concat.apply([], this); };
// Flatten nested arrarys // Example: flatten([[[[[0]], [1]], [[[2], [3]]], [[4], [5]]]]) // [0, 1, 2, 3, 4, 5] // using function 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 {/*from w ww.j a va2s.c o m*/ ret.push(ary[i]); } } return ret; } // using array method 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 flatten (){ var result = []; function recursiveArrTraverse(arr){ for (var index in arr) { if (Array.isArray(arr[index])){ recursiveArrTraverse(arr[index]); }else {/* w w w.j a v a2s. c o m*/ result.push(arr[index]); } } result.splice(result.length-1, 1); } recursiveArrTraverse(this); return result; } Array.prototype.flatten = flatten; var array = [1, 2, 3, 4]; console.log(array.flatten()); console.log(); array = [1, 2, [3, 4], [5, 6]]; console.log(array.flatten()); console.log(array); // Not changed console.log(); array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten());
Array.prototype.flatten = function () { var thisArr = this; var newArr = []; recursive(thisArr);//from w ww . j av a 2 s .c o m function recursive (arr) { for (var i = 0; i < arr.length; i++) { if (arr[i] instanceof Array) { recursive(arr[i]); } else { newArr.push(arr[i]); } } } return newArr; }; 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 newArray = []; function extracts(arr){ for (var index = 0; index < arr.length; index++) { var value = arr[index]; if (value instanceof Array) { extracts(value);/*from w ww.j av a 2 s .c om*/ } else { newArray.push(value); } } } extracts(this); return newArray; } var array = [1, 2, 3, 4]; console.log(array.flatten()); console.log(); var array = [1, 2, [3, 4], [5, 6]]; console.log(array.flatten()); console.log(array); // Not changed console.log(); var array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten()); console.log(array);
Array.prototype.flatten = function() { return this.inject([], function(array, value) { if (DiaFluxUtils.isArray(value)) return array.concat(value.flatten()); array.push(value);/* w w w . j a v a 2s . c o m*/ return array; }); }
Array.prototype.flatten = function () { var arrayResult = []; flatten(this);/* www . j ava 2s . c om*/ function flatten(array) { var length = array.length; for (var i = 0; i < length; i++) { var element = array[i]; if (Array.isArray(element)) { flatten(element); continue; } arrayResult.push(element); } } return arrayResult; }; var array; array = [1, 2, 3, 4]; console.log(array.flatten()); array = [1, 2, [3, 4], [5, 6]]; console.log(array.flatten()); console.log(array); // Not changed array = [undefined, 0, ['string', 'values'], 5.5, [[1, 2, true], [3, 4, false]], 10, {}, null, ' ']; console.log(array.flatten());
Array.prototype.flatten = function() { var results = []; for (var i = 0, len = this.length; i < len; i++) { if (typeof this[i] == "object" && this[i].constructor === Array) { results = results.concat(this[i].flatten()); } else {//from ww w. java2 s . co m results.push(this[i]); } } return results; }; // refactored Array.prototype.flatten = function() { var results = []; for (var i = 0, len = this.length; i < len; i++) { results = results.concat(Object.isArray(this[i]) ? this[i].flatten() : [this[i]]); } return results; }; // refactored (requires Enumerable) Array.prototype.flatten = function() { return this.inject([], function(array, value) { return array.concat(Object.isArray(value) ? value.flatten() : [value]); }); };
Array.prototype.flatten = function() { var originalArray = this; var newArray = []; var i = 0;//from w w w .ja v a2 s . c o m var j = 0; var element; var temp; for (i = 0; i < originalArray.length; i+=1) { element = originalArray[i]; if (element.constructor !== Array) { newArray.push(element); } else { temp = element.flatten(); for (j = 0; j < temp.length; j++) { newArray.push(temp[j]); } } } return newArray; } var array; array = [1, 2, 3, 4]; console.log(array.flatten()) array = [1, 2, [3, 4], [5, 6]]; console.log(array.flatten()); console.log(array); // Not changed array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten());
Array.prototype.flatten = function() { var originalArray = this, newArray = [],/* ww w . j a v a2 s .com*/ i = 0, j = 0, element, temp; for (i = 0; i < originalArray.length; i+=1) { element = originalArray[i]; if (element.constructor !== Array) { newArray.push(element); } else { temp = element.flatten(); for (j = 0; j < temp.length; j++) { newArray.push(temp[j]); } } } return newArray; } var array; array = [1, 2, 3, 4]; console.log(array.flatten()) array = [1, 2, [3, 4], [5, 6]]; console.log(array.flatten()); console.log(array); // Not changed array = [0, ['string', 'values'], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten());
var array = null; Array.prototype.flatten = function doFlattening(){ var newArr = this; var hasArray = true; var i = 0;/* www .j av a 2s .c o m*/ while(hasArray === true){ hasArray = false; for(i = 0; i < newArr.length; i += 1){ if(Array.isArray(newArr[i]) === true){ hasArray = true; } } newArr = [].concat.apply([], newArr); } return newArr; }; array = [1, 2, 3, 4]; console.log(array.flatten()); console.log(); array = [1, 2, [3, 4], [5, 6]]; console.log("Flattenned: "); console.log(array.flatten()); console.log(); console.log("Not flattenned: "); console.log(array); // Not changed console.log(); array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten());
Array.prototype.flatten = function() { return this.reduce(function(res, el) { if(el.constructor == Array) { el.forEach(function(sub_el) { res.push(sub_el);//from w ww . j a v a2s . c om }) } else { res.push(el); } return res; }, []); }
Array.prototype.flatten = function flatten(){ var result = []; for (var i = 0; i < this.length; i++){ if (this[i] instanceof Array){ for (var j = 0; j < this[i].length; j++){ if (this[i][j] instanceof Array){ for (var k = 0; k < this[i][j].length; k++){ result.push(this[i][j][k]); }// w w w . ja v a2 s . c om } else { result.push(this[i][j]); } } } else { result.push(this[i]); } } return result; } var array; array = [1, 2, 3, 4]; console.log(array.flatten()); array = [1, 2, [3, 4], [5, 6]]; console.log(array.flatten()); console.log(array); array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array.flatten());
/*Implement a function flatten() which works on an array of arrays and returns a new array (does not modify the original one). The function extracts all inner arrays and merges all values into a single-dimensional array.*/ Array.prototype.flatten = function() { var resultArray = []; innerFlatten(this);//from w ww.j av a 2 s .c o m function innerFlatten(arr) { arr.forEach(function(el) { if(Array.isArray(el)) { innerFlatten(el); } else { resultArray.push(el); } }); } return resultArray; }; var array = [1, 2, 3, 4]; console.log(array.flatten());// Not changed var array = [1, 2, [3, 4], [5, 6]]; console.log(array.flatten()); var array = [0, ["string", "values"], 5.5, [[[1, 2], true], [3, 4, false]], 10]; console.log(array.flatten());
Array.prototype.flatten = function () { //from w w w .ja v a 2s .c o m function flattenArrayOfArrays(array, result) { if (!result) { result = []; } for (var i = 0; i < array.length; i++) { if (Array.isArray(array[i])) { flattenArrayOfArrays(array[i], result); } else { result.push(array[i]); } } return result; } return flattenArrayOfArrays(this); } console.log("First sample code"); var array = [1, 2, 3, 4]; console.log(array); console.log(array.flatten()); console.log(); console.log("Second sample code"); var array = [1, 2, [3, 4], [5, 6]]; console.log(array); console.log(array.flatten()); console.log(); console.log("Third sample code"); var array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10]; console.log(array); console.log(array.flatten()); console.log();