Nodejs Array Flatten flaten()

Here you can find the source of flaten()

Method Source Code

'use strict';/*ww w. j  a  v  a2s  .  c o m*/

// TASK 03
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];

Array.prototype.flaten = function() {
    var flatenArray = [];

    var extractArray = function(arr) {
        if (arr.constructor === Array) {
            for (var i = 0; i < arr.length; i += 1) {
                extractArray(arr[i]);
            }
        } else {
            flatenArray.push(arr);
        }

        return flatenArray;
    }

    return extractArray(this);
}

// TESTS

// console.log(array); // ORIGINAL ARRAY IS NOT TOUCHED
console.log(array.flaten());

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. flatten()
    Array.prototype.flatten = function() {
        return this.reduce(function (a, b) { return a.concat(b instanceof Array ? b.flatten() : b); }, []);
    };
    
  3. flatten()
    Array.prototype.flatten = function () {
      var flattened = Array.prototype.concat.apply([], this);
      if (flattened.length == this.length) return flattened;
      else return flattened.flatten();
    
  4. flatten()
    Array.prototype.flatten = function() {
        return Array.prototype.concat.apply([], this);
    };
    
  5. flatten()
    Array.prototype.flatten = function() {
        "use strict";
        var flattened = [];
        flattened.concat.apply(flattened, this);
    };