Nodejs Array Flatten flatten()

Here you can find the source of flatten()

Method Source Code

isArray =  function(obj) {
    return toString.call(obj) === '[object Array]';
};

var flatten = function(arr) {
    return arr.reduce(function(memo, value) {
           if (isArray(value)) {
               return memo.concat(flatten(value));
           }/* w ww .j ava 2s .c  o m*/
           else {
               memo[memo.length] = value;
               return memo;
           }
            }, []);
};

Array.prototype.flatten = function(){
    return flatten(this);  
};
Array.prototype.contains = function(item){
    return (this.indexOf(item) != -1);
};

module.exports = {
    prototype : Array.prototype
};

Related

  1. flatten()
    Array.prototype.flatten = function () { 
      return this.map((a) => (a.constructor.name == 'Array' && a.length == 1) ? a[0] : a) 
    
  2. flatten()
    Array.prototype.flatten = function () {
        var flattenedArray = [];
        function copyArray(arr) {
            for (var i = 0; i < arr.length; i += 1) {
                if (arr[i] instanceof Array) {
                    copyArray(arr[i]);
                } else {
                    flattenedArray.push(arr[i]);
        copyArray(this);
        return flattenedArray;
    };
    var arr1 = [1, 2, [3, 4], 5];
    console.log('Original:');
    console.log(arr1);
    console.log();
    console.log('Flattened:');
    console.log(arr1.flatten());
    
  3. flatten()
    var outputArray = [];
    Array.prototype.flatten = function (){
        function getValues(array){
            var i;
            for (i = 0; i < array.length; i += 1){
                var value = array[i];
                if (Array.isArray(value)){
                    getValues(value);
                else{
                    outputArray.push(value);
        getValues(this);
        return outputArray;
    var array = [0, ["string", "values"], 5.5, [[1, 2, true], [3, 4, false]], 10];
    console.log(array.flatten());
    
  4. flatten()
    function flatten (arrays, call) {
      call = call + 1 || 0;
      if(arrays.length === 0 && !call) return null;
      return arrays.reduce(function(flat, cur){
        if(!Array.isArray(cur)) return flat.concat(cur);
        return flat.concat(flatten(cur, call));
      }, []);
    Array.prototype.flatten = function(){
    ...
    
  5. flatten()
    Array.prototype.flatten = function() {
        var newArray = [];
        function extractValues( arr ) {
            for (var i = 0, len = arr.length; i < len; i++) {
                var val = arr[ i ];
                if (val instanceof Array) {
                    extractValues( val );
                } else {
                    newArray.push( val );
    ...
    
  6. flatten()
    Array.prototype.flatten = function(){
        var flattenedArray = [];
        function flattenArrays(arr) {
            for (var i = 0; i < arr.length; i++) {
                var currentValue = arr[i];
                if(Array.isArray(currentValue)){
                    flattenArrays(currentValue);
                }else{
                    flattenedArray.push(currentValue);
    ...
    
  7. flatten()
    Array.prototype.flatten = function() {
      var merged = []
      return merged.concat.apply(merged, this)
    
  8. flatten()
    Array.prototype.flatten = function() {
      var resultArr = [];
      function getArrays (arr) {
        for (var i = 0; len = arr.length, i < len; i += 1) {
          var value = arr[i];  
          if (Array.isArray(value)) {
            getArrays(value);
          } else {
            resultArr.push(value);
    ...
    
  9. flatten()
    var array = [1,[4], [9, 16, 25], [36, 49, [64, 81]], [100, [121, 144, [169]]], [196, [225, [256, 289, [324, [361, 400]]]]]];
    Array.prototype.flatten = function() {
        var ret = [];
        for(var i = 0; i < this.length; i++) {
            if(Array.isArray(this[i])) {
                ret = ret.concat(this[i].flatten());
            } else {
                ret.push(this[i]);
        return ret;
    };
    function isSquare(arr){
      result = arr.flatten();
      for(var i = 0; i<result.length; i++){
        if(Math.sqrt(result[i]) % 1 != 0){
          return false; 
      return true;
    debug(isSquare(array));