Nodejs Array Flatten flatten()

Here you can find the source of flatten()

Method Source Code

Array.prototype.flatten = function () {
    var arrayResult = [];
    flatten(this);//from w  w w  .  jav a2s. c  om

    function flatten(array) {
        var length = array.length;
        for (var i = 0; i < length; i++) {
            var element = array[i];
            if (Array.isArray(element)) {
                flatten(element);
                continue;
            }

            arrayResult.push(element);
        }
    }

    return arrayResult;
};

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

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


array = [undefined, 0, ['string', 'values'], 5.5, [[1, 2, true], [3, 4, false]], 10, {}, null, ' '];
console.log(array.flatten());

Related

  1. flatten()
    Array.prototype.flatten = function() {
        var resultArray = [];
        function getValues(array) {
            var i;
            for (i = 0; i < array.length; i += 1) {
                var value = array[i];
                if (Array.isArray(value)) {
                    getValues(value);
                } else {
    ...
    
  2. flatten()
    Array.prototype.flatten = function () {
        var thisArr = this;
        var newArr  = [];
        recursive(thisArr);
        function recursive (arr) {
            for (var i = 0; i < arr.length; i++) {
                if (arr[i] instanceof Array) {
                    recursive(arr[i]);
                } else {
    ...
    
  3. flatten()
    "use strict";
    Array.prototype.flatten = function () {
        var result = [];
        function getValues(array) {
            var i;
            for (i = 0; i < array.length; i++) {
                var value = array[i];
                if (Array.isArray(value)) {
                    getValues(value);
    ...
    
  4. flatten()
    var array;
    Array.prototype.flatten = function () {
        'use strict';
        var resultArray = [];
        function getValues(array) {
            var i,
                value;
            for (i = 0; i < array.length; i += 1) {
                value = array[i];
    ...
    
  5. flatten()
    Array.prototype.flatten = function() {
        var resultArray = [];
        function getValues(array) {
            var i;
            for (i = 0; i < array.length; i += 1) {
                var value = array[i];
                if (Array.isArray(value)) {
                    getValues(value);
                } else {
    ...
    
  6. flatten()
    Array.prototype.flatten = function() {
        var flatten = function(items) {
            var flattened = [];
            items.forEach(function(item) {
                if (typeof item === "object") {
                    flatten(item).forEach(function (i) {
                        flattened.push(i);
                    });
                } else {
    ...
    
  7. flatten()
    Array.prototype.flatten = function(){
      var flat_array = [];
      for(var i = 0; i < this.length; i++){
        if (this[i].constructor == Array){
          this[i] = this[i].flatten();
          flat_array = flat_array.concat(this[i]);
        else { flat_array.push(this[i]); }
      return flat_array;
    
  8. flatten()
    Array.prototype.flatten = function() {
      var results = [];
      for (var i = 0, len = this.length; i < len; i++) {
        if (typeof this[i] == "object" && this[i].constructor === Array) {
          results = results.concat(this[i].flatten());
        } else {
          results.push(this[i]);
      return results;
    };
    Array.prototype.flatten = function() {
      var results = [];
      for (var i = 0, len = this.length; i < len; i++) {
        results = results.concat(Object.isArray(this[i]) ? this[i].flatten() : [this[i]]);
      return results;
    };
    Array.prototype.flatten = function() {
      return this.inject([], function(array, value) {
        return array.concat(Object.isArray(value) ?
          value.flatten() : [value]);
      });
    };
    
  9. flatten()
    Array.prototype.flatten = function() {
        var resultArray = [];
        function getValues(array) {
            var i;
            for (i = 0; i < array.length; i += 1) {
                var value = array[i];
                if (Array.isArray(value)) {
                    getValues(value);
                } else {
    ...