Nodejs Array Flatten flatten()

Here you can find the source of flatten()

Method Source Code

"use strict";/*from  ww w.j  av  a  2s. c om*/

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);
            }
            else {
                result.push(value);
            }
        }
    }
    getValues(this);
    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); // Not changed
array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10];
console.log(array.flatten());

Related

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