Nodejs Array Flatten flatten()

Here you can find the source of flatten()

Method Source Code

/*Implement a function flatten() which works on an array of arrays and returns a new array (does not modify the original one).
  The function extracts all inner arrays and merges all values into a single-dimensional array.*/

Array.prototype.flatten = function() {
    var resultArray = [];
    innerFlatten(this);//  w w  w.j a va 2s  . c om
    function innerFlatten(arr) {
        arr.forEach(function(el) {
            if(Array.isArray(el)) {
                innerFlatten(el);
            } else {
                resultArray.push(el);
            }
        });
    }

    return resultArray;
};

var array = [1, 2, 3, 4];
console.log(array.flatten());// Not changed
var array = [1, 2, [3, 4], [5, 6]];
console.log(array.flatten());
var array = [0, ["string", "values"], 5.5, [[[1, 2], true], [3, 4, false]], 10];
console.log(array.flatten());

Related

  1. 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]);
      });
    };
    
  2. 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 {
    ...
    
  3. flatten()
    Array.prototype.flatten = function() {
      var toString = Object.prototype.toString;
      var arrayTypeStr = '[object Array]';
      var result = [];
      var nodes = this.slice();
      var node;
      if (!this.length) {
        return result;
      node = nodes.pop();
      do {
        if (toString.call(node) === arrayTypeStr) {
          nodes.push.apply(nodes, node);
        } else {
          result.push(node);
      } while (nodes.length && (node = nodes.pop()) !== undefined);
      result.reverse();
      return result;
    
  4. flatten()
    Array.prototype.flatten = function() {
        var originalArray = this,
            newArray = [],
            i = 0,
            j = 0,
            element,
            temp;
        for (i = 0; i < originalArray.length; i+=1) {
            element = originalArray[i];
    ...
    
  5. 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];
    ...
    
  6. 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}
      ]
    ...
    
  7. flatten()
    Array.prototype.flatten = function(){
      var result = [];
      this.forEach(function(x) {
        result = result.concat(x);
      });
      return result;
    };
    
  8. flatten()
    Array.prototype.flatten = function () {
        return this.reduce((p,c) => p.concat(c), [])
    
  9. flatten()
    Array.prototype.flatten = function () {
        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 {
    ...