Nodejs Array Flatten flatten()

Here you can find the source of flatten()

Method Source Code

Array.prototype.flatten = function(){
    var flattenArray = [];
    for (var el in this) {
        var currElement = this[el];
        if (Object.prototype.toString.call(currElement) === '[object Array]' ) {
            for (var i in currElement) {
                flattenArray.push(currElement[i]);
            }/*from   ww  w.j a v a2 s  .com*/
        } else {
            flattenArray.push(currElement);
        }
    }
    deleteFunctions(flattenArray);
    return flattenArray;
};


function deleteFunctions(arr){
    for (var k in arr) {
        var element = arr[k];
        if (isFunction(element)) {
            var index = arr.indexOf(element);
            if (index > -1) {
                arr.splice(index, 1);
            }
        }
    }
}

function isFunction(functionToCheck) {
    var getType = {};
    return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
}

var array = [1, 2, [3, 4], [5, 6]];
console.log(array.flatten());
console.log(array);

var array1 = [1, 2, 3, 4];
console.log(array1.flatten());
console.log(array1);

var array2 = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10];
console.log(array2.flatten());
console.log(array2);

Related

  1. flatten()
    Array.prototype.flatten = function () {
        return this.reduce((p,c) => p.concat(c), [])
    
  2. flatten()
    Array.prototype.flatten = function () {
        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 {
    ...
    
  3. flatten()
    Array.prototype.flatten = function() {
      return this.reduce(function(res, el) {
        if(el.constructor == Array) {
          el.forEach(function(sub_el) {
            res.push(sub_el);
          })
        } else {
          res.push(el);
        return res;
      }, []);
    
  4. flatten()
    Array.prototype.flatten = function () {
      var toReturn = [];
      for (var i = 0, len = this.length; i < len; i++) {
        if (this[i] instanceof Array) {
          toReturn = toReturn.concat(this[i].flatten());
        } else {
          toReturn.push(this[i]);
      return toReturn;
    };
    
  5. flatten()
    Array.prototype.flatten = function() {
      return this.reduce(function(a,b) {
        return a.concat(b)
      }, [])
    
  6. flatten()
    var slice = Array.prototype.slice;
    Array.prototype.flatten = function () {
      var obj, result = [];
      for (var i = 0, l = this.length; i < l; ++i ) {
        obj = this[ i ];
        if ( Array.isArray( obj ) ) {
          result = result.concat( obj.flatten() );
        } else {
          result[ result.length ] = obj;
    ...
    
  7. flatten()
    Array.prototype.flatten = Array.prototype.flatten || function() {
      'use strict';
      var that = this;
      for (var i = 0; i < that.length; i += 1) {
        if (Array.isArray(that[i])) {
          that = that.concat.apply([], that);
          i -= 1;
      return that;
    };
    Array.prototype.flatten1 = Array.prototype.flatten1 || function() {
      'use strict';
      var res = [];
      (function flatten(arr) {
        for (var i = 0; i < arr.length; i += 1) {
          if (Array.isArray(arr[i])) {
            flatten(arr[i]);
          } else {
            res.push(arr[i]);
      }(this));
      return res;
    };
    Array.prototype.flatten2 = Array.prototype.flatten2 || function() {
      'use strict';
      for (var i = 0; i < this.length; i += 1) {
        if (Array.isArray(this[i])) {
          this.splice.apply(this, [i, 1].concat(this[i].flatten()));
      return this;
    };
    var a = [1, 2, [3, 4, [5, 6]]];
    console.log(a.flatten()); 
    console.log(a); 
    console.log(a.flatten1()); 
    console.log(a); 
    console.log(a.flatten2()); 
    console.log(a); 
    
  8. flatten()
    var arrays = [[1, 2, 3], [4, 5], [6]];
    console.log(arrays.reduce(function(a, b){
      return a.concat(b);
    }));
    Array.prototype.flatten = function(){
      return this.reduce(function(a, b){
        if(!Array.prototype.isPrototypeOf(a))
          a = [a];
        else
    ...
    
  9. flatten(array)
    Array.prototype.flatten = function(array) {
      return this.reduce((prev, next) => prev.concat(next), []);
    };