Nodejs Array Flatten flatten()

Here you can find the source of flatten()

Method Source Code

Array.prototype.flatten = function (){
    var array = this;
    var newArray = [];
    recursive(array);/*from   www.  j a  v a  2s  .  c o  m*/

    function recursive (arr){
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] instanceof Array) {
                recursive(arr[i]);
            } else {
                newArray.push(arr[i]);
            };
        }
    }
    return newArray;
}

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

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

Related

  1. flatten()
    var array = [1,[4], [9, 16, 25], [36, 49, [64, 81]], [100, [121, 144, [169]]], [196, [225, [256, 289, [324, [361, 400]]]]]];
    Array.prototype.flatten = function() {
        var ret = [];
        for(var i = 0; i < this.length; i++) {
            if(Array.isArray(this[i])) {
                ret = ret.concat(this[i].flatten());
            } else {
                ret.push(this[i]);
        return ret;
    };
    function isSquare(arr){
      result = arr.flatten();
      for(var i = 0; i<result.length; i++){
        if(Math.sqrt(result[i]) % 1 != 0){
          return false; 
      return true;
    debug(isSquare(array));
    
  2. flatten()
    Array.prototype.flatten = function(){
      return flatten(this);
    };
    function flatten(arr) {
      return arr.reduce(function (flat, toFlatten) {
        return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
      }, []);
    console.log(flatten([[1,2],[3,4]]));
    ...
    
  3. flatten()
    'use strict';
    Array.prototype.flatten = function() {
        var resultArray = [];
        for (var i = 0; i < this.length; i++) {
            if (this[i] instanceof Array) {
                resultArray = resultArray.concat(this[i].flatten());
            } else {
                resultArray.push(this[i]);
        return resultArray;
    };
    var array = [1, 2, 3, 4];
    console.log(array.flatten());
    var array = [1, 2, [3, 4], [5, 6]];
    console.log(array.flatten());
    console.log(array); 
    var array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10];
    console.log(array.flatten());
    
  4. flatten()
    Array.prototype.flatten = function() {
        var result = [];
        function checkForArrays(arr) {
            arr.forEach(function(el) {
                if (Array.isArray(el)) {
                    checkForArrays(el);
                }else {
                    result.push(el);
            })
        checkForArrays(this);
        return result;
    };
    var array = [1, 2, 3, 4];
    console.log(array.flatten());
    
  5. flatten()
    function flatten (ary) {
      var ret = []
      for (var i = 0; i < ary.length; i++) {
        if (Array.isArray(ary[i])) {
          ret = ret.concat(flatten(ary[i]))
        } else {
          ret.push(ary[i])
      return ret
    Array.prototype.flatten = function () {
      var ret = []
      for (var i = 0; i < this.length; i++) {
        if (Array.isArray(this[i])) {
          ret = ret.concat(this[i].flatten())
        } else {
          ret.push(this[i])
      return ret
    function flattenReduce (ary) {
      return ary.reduce(function (a, b) {
        if (Array.isArray(b)) {
          return a.concat(flattenReduce(b))
        return a.concat(b)
      }, [])
    
  6. flatten()
    Array.prototype.flatten = function(){
        var array = this;
        var processedArray = [];
        for(var i = 0; i < array.length; i++){
            if(array[i] instanceof Array){
                var subArray = array[i].flatten();
                processedArray.push.apply(processedArray,subArray);
            else {
    ...
    
  7. flatten()
    Array.prototype.flatten = function() {
        function getArrayElements(array, result) {
            if(!result) {
                result = [];
            for (var i=0; i<array.length; i++) {
                if(Array.isArray(array[i])) {
                    getArrayElements(array[i], result);
                }else {
    ...
    
  8. flatten()
    Array.prototype.flatten = function() {
        return this.inject([], function(array, value) {
          if (DiaFluxUtils.isArray(value))
            return array.concat(value.flatten());
          array.push(value);
          return array;
        });
    
  9. flatten()
    Array.prototype.flatten = function(){
      return this.reduce(function(flat, val, i, hilly){
        Array.isArray(val) ? flat.concat(val.flatten()) : flat.concat(val);
      }, []);