Nodejs Array Flatten flatten()

Here you can find the source of flatten()

Method Source Code

//https://egghead.io/lessons/javascript-create-an-array-concatall-method

//nested data structure representing stock exchanges
var exchanges = [
   [//  w  ww  . j  a va2  s . c  o  m
      {symbol: "JNJ", price: 2030.00, volume:48984}, 
       {symbol: "PWC", price: 4340.00, volume:15985}
   ],
   [
      {symbol: "CISC", price: 4563.00, volume:874651},
      {symbol: "KROG", price: 712.00, volume:8949}
   ]
];


Array.prototype.flatten = function() {
   var results = [];
   (function recur(obj) {
      if(Array.isArray(obj)) {
         obj.forEach(function(element) {
            recur(element);
         });
      } else {
         results.push(obj);
      }      
   })(this);
   return results;
};

console.log(exchanges.
            flatten().
            filter(function(stock) {
               return stock.price > 2500.00;
            }).
            map(function(stock) {
               return stock.price;
            }).
            reduce((a, b) => (a + b), 0)   //like a fold operation
         );

Related

  1. 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 {
    ...
    
  2. flatten()
    Array.prototype.flatten = function() {
      var toString = Object.prototype.toString;
      var arrayTypeStr = '[object Array]';
      var result = [];
      var nodes = this.slice();
      var node;
      if (!this.length) {
        return result;
      node = nodes.pop();
      do {
        if (toString.call(node) === arrayTypeStr) {
          nodes.push.apply(nodes, node);
        } else {
          result.push(node);
      } while (nodes.length && (node = nodes.pop()) !== undefined);
      result.reverse();
      return result;
    
  3. flatten()
    Array.prototype.flatten = function() {
        var originalArray = this,
            newArray = [],
            i = 0,
            j = 0,
            element,
            temp;
        for (i = 0; i < originalArray.length; i+=1) {
            element = originalArray[i];
    ...
    
  4. flatten()
    Array.prototype.flatten = function() {
        var originalArray = this;
        var newArray = [];
        var i = 0;
        var j = 0;
        var element;
        var temp;
        for (i = 0; i < originalArray.length; i+=1) {
            element = originalArray[i];
    ...
    
  5. flatten()
    Array.prototype.flatten = function() {
        var resultArray = [];
        innerFlatten(this);
        function innerFlatten(arr) {
            arr.forEach(function(el) {
                if(Array.isArray(el)) {
                    innerFlatten(el);
                } else {
                    resultArray.push(el);
    ...
    
  6. flatten()
    Array.prototype.flatten = function(){
      var result = [];
      this.forEach(function(x) {
        result = result.concat(x);
      });
      return result;
    };
    
  7. flatten()
    Array.prototype.flatten = function () {
        return this.reduce((p,c) => p.concat(c), [])
    
  8. flatten()
    Array.prototype.flatten = function () {
        function flattenArrayOfArrays(array, result) {
            if (!result) {
                result = [];
            for (var i = 0; i < array.length; i++) {
                if (Array.isArray(array[i])) {
                    flattenArrayOfArrays(array[i], result);
                } else {
    ...
    
  9. flatten()
    Array.prototype.flatten = function() {
      return this.reduce(function(res, el) {
        if(el.constructor == Array) {
          el.forEach(function(sub_el) {
            res.push(sub_el);
          })
        } else {
          res.push(el);
        return res;
      }, []);