Nodejs Array Flatten flatten()

Here you can find the source of flatten()

Method Source Code

Array.prototype.flatten = function () {
    //from  w w  w .  j av  a2s  .c  o  m
    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 {
                result.push(array[i]);
            }
        }
        return result;
    } 
    return flattenArrayOfArrays(this);
}

console.log("First sample code");
var array = [1, 2, 3, 4];
console.log(array);
console.log(array.flatten());
console.log();

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

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

Related

  1. flatten()
    Array.prototype.flatten = function() {
        var originalArray = this;
        var newArray = [];
        var i = 0;
        var j = 0;
        var element;
        var temp;
        for (i = 0; i < originalArray.length; i+=1) {
            element = originalArray[i];
    ...
    
  2. flatten()
    Array.prototype.flatten = function() {
        var resultArray = [];
        innerFlatten(this);
        function innerFlatten(arr) {
            arr.forEach(function(el) {
                if(Array.isArray(el)) {
                    innerFlatten(el);
                } else {
                    resultArray.push(el);
    ...
    
  3. flatten()
    var exchanges = [
      [
        {symbol: "JNJ", price: 2030.00, volume:48984}, 
         {symbol: "PWC", price: 4340.00, volume:15985}
      ],
      [
        {symbol: "CISC", price: 4563.00, volume:874651},
        {symbol: "KROG", price: 712.00, volume:8949}
      ]
    ...
    
  4. flatten()
    Array.prototype.flatten = function(){
      var result = [];
      this.forEach(function(x) {
        result = result.concat(x);
      });
      return result;
    };
    
  5. flatten()
    Array.prototype.flatten = function () {
        return this.reduce((p,c) => p.concat(c), [])
    
  6. 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;
      }, []);
    
  7. 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;
    };
    
  8. flatten()
    Array.prototype.flatten = function() {
      return this.reduce(function(a,b) {
        return a.concat(b)
      }, [])
    
  9. flatten()
    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]);
            } else {
    ...