Nodejs Array Flatten flatten()

Here you can find the source of flatten()

Method Source Code

// 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);
};

Related

  1. flat()
    Array.prototype.flat = function() {
      return this.reduce(function(a, b) {
        return a.concat(b);
      }, []);
    };
    var range = function(n) {
      var ret = [];
      for (var i = 0; i < n; i++) {
        ret.push(i);
    ...
    
  2. flaten()
    'use strict';
    var array = [1, 2, 3, 4];
    var array = [1, 2, [3, 4],
        [5, 6]
    ];
    var array = [0, ["string", "values"], 5.5, [
        [1, 2, true],
        [3, 4, false]
    ], 10];
    ...
    
  3. flatten()
    Array.prototype.flatten = function() {
        return this.reduce(function (a, b) { return a.concat(b instanceof Array ? b.flatten() : b); }, []);
    };
    
  4. flatten()
    Array.prototype.flatten = function () {
      var flattened = Array.prototype.concat.apply([], this);
      if (flattened.length == this.length) return flattened;
      else return flattened.flatten();
    
  5. flatten()
    Array.prototype.flatten = function() {
        "use strict";
        var flattened = [];
        flattened.concat.apply(flattened, this);
    };
    
  6. 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());
    ...
    
  7. 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;
    ...
    
  8. flatten()
    Array.prototype.flatten = function () { 
      return this.map((a) => (a.constructor.name == 'Array' && a.length == 1) ? a[0] : a)