Nodejs Array Flat Map flatMap(callback)

Here you can find the source of flatMap(callback)

Method Source Code

Array.prototype.flatMap = function(callback) {
  var inputArray = this;
  var start = function(value, index, length) {
    flatEach(value.shift());/*from w w  w.  j av a2s.  co m*/
    ++index != length && start(value, index, length);
    return value;
  }

  var flatEach = function(eachValue) {
    return Array.isArray(eachValue) ? start(eachValue, 0, eachValue.length) : inputArray.push(callback(eachValue));
  }

  return start(inputArray, 0, inputArray.length);
}

Related

  1. flatMap(cb)
    Array.prototype.flatMap = function (cb) {
      return this.map(cb).reduce(function (arr, c) { return arr.concat(c); }, []);
    };
    
  2. flatMap(fn, ctx)
    Array.prototype.flatMap = function(fn, ctx) {
      return this.reduce((k, v) => k.concat(fn.call(ctx, v)), []);
    };
    
  3. flatMap(lambda)
    Array.prototype.flatMap = function(lambda) { 
          return Array.prototype.concat.apply([], this.map(lambda)); 
    };
    
  4. flatMap(lambda)
    Array.prototype.flatMap = function(lambda) {
        return [].concat(this.map(lambda));
    };