Javascript Array flattenArray()
method
function flattenArray(a, b) { return a.concat(b); } Array.prototype.flattenArray = function() { return this.reduce(flattenArray); } var arrays = [[1, 2, 3], [4, 5], [6]]; console.log(arrays.flattenArray());/*from ww w.j av a 2s . com*/
Array.prototype.flattenArray = function() { var flatArray = arrays.reduce(function(a, b) { return a.concat(b); }, []);/*from www. j av a 2s .c o m*/ console.log(flatArray); return ""; } var arrays = [[1, 2, 3], [4, 5], [6]]; console.log(arrays.flattenArray())
Array.prototype.flattenArray = function (){ var array = this.reduce(function(a, b) {return a.concat(b);}, []); return array;/*from www. j a v a 2 s . c o m*/ } //------------------- Testing -----------------------------------// var arrays = [[2, 3, 4], [1, 2], [2]]; console.log( arrays.flattenArray() ); //------------------- Testing -----------------------------------//
var arrays = [[1, 2, 3], [4, 5], [6]]; var newArray = []; Array.prototype.flattenArray = function() { newArray = this.reduce(concatArray)//from ww w . j av a2 s .com return newArray; }; function concatArray(previousValue, currentValue){ return previousValue.concat(currentValue); } console.log(arrays.flattenArray());
Array.prototype.flattenArray = function() { var rescultArray=[]; this.reduce(function(pre, cur,index) {/* www. j a v a 2s . c o m*/ rescultArray=index==1?rescultArray.concat(pre,cur):rescultArray.concat(cur); }); return rescultArray; } var arrays = [[1, 2, 3], [4, 5], [6]]; console.log(arrays.flattenArray());
var arrays = [[1, 2, 3], [4, 5], [6]]; Array.prototype.flattenArray = function() { // arrays.reduce(function(i,j) {return i.concat(j);} ,[]); arrays.reduce(function(previousValue, currentValue, index, array) { console.log(previousValue+"---"+currentValue); return previousValue.concat(previousValue); });//from w w w .j a v a 2 s .c o m } console.log(arrays.flattenArray);
Array.prototype.flatten = function flattenArray() { var result = []; function traverseArr(array) { for (var i = 0; i < array.length; i++) { if (array[i] instanceof Array) { traverseArr(array[i]);/*from w w w . j a va 2 s. c om*/ } else { result.push(array[i]); } } } traverseArr(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());
/*/*from w w w. jav a 2 s. co m*/ Write a function named flattenArray that uses 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. This function should apply to all arrays. (Hint: use Array.prototype to define your function) var arrays = [[1, 2, 3], [4, 5], [6]]; console.log( arrays.flattenArray ) // ! [1, 2, 3, 4, 5, 6] Your code should execute the code above. */ Array.prototype.flattenArray = function() { return this.reduce(function(previous, current){ return previous.concat(current); }); }; var arrays = [[1, 2, 3], [4, 5], [6]]; console.log(arrays.flattenArray()); // ! [1, 2, 3, 4, 5, 6]