Nodejs Array Flatten flatten()

Here you can find the source of flatten()

Method Source Code

/**/*from  ww w  .  j av  a  2s  . co  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);
   }, []);
}

Related

  1. flatten()
    Array.prototype.flatten = function () {
      return this.reduce(function (flat, toFlatten) {
          return flat.concat(Array.isArray(toFlatten) ? toFlatten.flatten() : toFlatten);
      }, []);
    var array = [1, 2, 3, 4];
    console.log(array.flatten());
    var array = [1, 2, [3, 4], [5, 6]];
    console.log(array.flatten());
    ...
    
  2. flatten()
    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]);
      return flat;
    ...
    
  3. flatten()
    Array.prototype.flatten = function () { 
      return this.map((a) => (a.constructor.name == 'Array' && a.length == 1) ? a[0] : a) 
    
  4. 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]);
                } 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());
    
  5. flatten()
    var outputArray = [];
    Array.prototype.flatten = function (){
        function getValues(array){
            var i;
            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());
    
  6. flatten()
    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 );
    ...
    
  7. flatten()
    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 {
    ...
    
  8. flatten()
    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);
    ...
    
  9. flatten()
    Array.prototype.flatten = function() {
      var merged = []
      return merged.concat.apply(merged, this)