Nodejs Array Flatten flattenArray()

Here you can find the source of flattenArray()

Method Source Code

Array.prototype.flattenArray = function()
{
   var rescultArray=[];
      this.reduce(function(pre, cur,index) 
      {//from w  w  w  .  ja v  a  2s . c  o  m
         rescultArray=index==1?rescultArray.concat(pre,cur):rescultArray.concat(cur);
       });

    return rescultArray;
}

var arrays = [[1, 2, 3], [4, 5], [6]];   
console.log(arrays.flattenArray());

Related

  1. flatten(func)
    Array.prototype.flatten = function (func) {
      var result = [];
      forEach(this, function (element) {
        forEach(func(element), function (child) {
          result.push(child);
        });
      });
      return result;  
    
  2. 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;
    };
    ...
    
  3. 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;
    
  4. 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());
    
  5. 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())
    ...
    
  6. flattenArray()
    Array.prototype.flattenArray =  function (){
      var array = this.reduce(function(a, b) {return a.concat(b);}, []);
      return array;
    var arrays = [[2, 3, 4], [1, 2], [2]];
    console.log( arrays.flattenArray() );
    
  7. flattenArray()
    var arrays = [[1, 2, 3], [4, 5], [6]];
    Array.prototype.flattenArray = function() 
          arrays.reduce(function(previousValue, currentValue, index, array) 
            console.log(previousValue+"---"+currentValue);
            return previousValue.concat(previousValue);
          });
    console.log(arrays.flattenArray);
    
  8. flattenInPlace()
    Array.fill = function(count, generator){
      var a = new Array(count)
      for(var i=0; i<count; i++){
        a[i] = generator(i)
      return a
    function isDefined(a){
      return (a !== null) && (a !== undefined)
    ...