Nodejs Array Flatten flatten(func)

Here you can find the source of flatten(func)

Method Source Code

Array.prototype.flatten = function (func) {
   var result = [];
   forEach(this, function (element) {
      forEach(func(element), function (child) {
         result.push(child);//from ww  w .  j a  v  a  2  s  .  c  o m
      });
   });
   return result;   
}

Related

  1. 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 {
    ...
    
  2. flatten()
    var slice = Array.prototype.slice;
    Array.prototype.flatten = function () {
      var obj, result = [];
      for (var i = 0, l = this.length; i < l; ++i ) {
        obj = this[ i ];
        if ( Array.isArray( obj ) ) {
          result = result.concat( obj.flatten() );
        } else {
          result[ result.length ] = obj;
    ...
    
  3. flatten()
    Array.prototype.flatten = Array.prototype.flatten || function() {
      'use strict';
      var that = this;
      for (var i = 0; i < that.length; i += 1) {
        if (Array.isArray(that[i])) {
          that = that.concat.apply([], that);
          i -= 1;
      return that;
    };
    Array.prototype.flatten1 = Array.prototype.flatten1 || function() {
      'use strict';
      var res = [];
      (function flatten(arr) {
        for (var i = 0; i < arr.length; i += 1) {
          if (Array.isArray(arr[i])) {
            flatten(arr[i]);
          } else {
            res.push(arr[i]);
      }(this));
      return res;
    };
    Array.prototype.flatten2 = Array.prototype.flatten2 || function() {
      'use strict';
      for (var i = 0; i < this.length; i += 1) {
        if (Array.isArray(this[i])) {
          this.splice.apply(this, [i, 1].concat(this[i].flatten()));
      return this;
    };
    var a = [1, 2, [3, 4, [5, 6]]];
    console.log(a.flatten()); 
    console.log(a); 
    console.log(a.flatten1()); 
    console.log(a); 
    console.log(a.flatten2()); 
    console.log(a); 
    
  4. flatten()
    var arrays = [[1, 2, 3], [4, 5], [6]];
    console.log(arrays.reduce(function(a, b){
      return a.concat(b);
    }));
    Array.prototype.flatten = function(){
      return this.reduce(function(a, b){
        if(!Array.prototype.isPrototypeOf(a))
          a = [a];
        else
    ...
    
  5. flatten(array)
    Array.prototype.flatten = function(array) {
      return this.reduce((prev, next) => prev.concat(next), []);
    };
    
  6. flatten(nil)
    Array.prototype.flatten = function(nil){
        result = new Array();
        for(var i = 0; i < this.length; i++){
      item = this[i];
      if($.isArray(item)) for(var j = 0; j < item.length; j++) result.push(item[j]);
      else result.push(item);
        return result;
    };
    ...
    
  7. flatten(prevous)
    Array.prototype.flatten = function(prevous) {
      var flattenedArr = prevous || [];
      for (var i = 0; i < this.length; i++) {
        if(Array.isArray(this[i])) {
          this[i].flatten(flattenedArr);
        } else if(this[i] === null) {
        } else {
          flattenedArr.push(this[i]);
      return flattenedArr;
    
  8. flattenArray()
    function flattenArray(a, b) {
      return a.concat(b);
    Array.prototype.flattenArray = function() {
      return this.reduce(flattenArray);
    var arrays = [[1, 2, 3], [4, 5], [6]];
    console.log(arrays.flattenArray());
    
  9. flattenArray()
    Array.prototype.flattenArray = function() {
      var flatArray = arrays.reduce(function(a, b) {
      return a.concat(b);
    }, []);
    console.log(flatArray);
    return "";
    var arrays = [[1, 2, 3], [4, 5], [6]];
    console.log(arrays.flattenArray())
    ...