Nodejs Array Flatten flatten()

Here you can find the source of flatten()

Method Source Code

Array.prototype.flatten = function () {
    var result = [];

    function flatten(elements, output) {
        elements.forEach(function (element) {
            if (!(typeof element === 'object' || typeof element === typeof Array)) {
                output.push(element);/*w  w  w . j  a  v a 2s.  co m*/
            }
            else {
                flatten(element, output);
            }
        });
    }

    flatten(this, result);
    return result;
};

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

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

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 array = this;
        var newArray = [];
        recursive(array);
        function recursive (arr){
            for (var i = 0; i < arr.length; i++) {
                if (arr[i] instanceof Array) {
                    recursive(arr[i]);
                } else {
    ...
    
  2. 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 {
    ...
    
  3. 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 {
    ...
    
  4. 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;
        });
    
  5. flatten()
    Array.prototype.flatten = function(){
      return this.reduce(function(flat, val, i, hilly){
        Array.isArray(val) ? flat.concat(val.flatten()) : flat.concat(val);
      }, []);
    
  6. flatten()
    Array.prototype.flatten = function() {
        return Array.prototype.concat.apply([], this);
    
  7. 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 {
    ...
    
  8. 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 {
    ...
    
  9. 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);
    ...