Nodejs Array Concatenate Map concatMap(projectionFunctionThatReturnsArray)

Here you can find the source of concatMap(projectionFunctionThatReturnsArray)

Method Source Code

Array.prototype.concatMap = function(projectionFunctionThatReturnsArray) {
   return this.//  w ww  .java  2 s  . c om
      map(function(item) {
         return this.map((i) => {
        return i;
      }).concatAll();
      }).
      // apply the concatAll function to flatten the two-dimensional array
      concatAll();
};

/*
   var spanishFrenchEnglishWords = [ ["cero","rien","zero"], ["uno","un","one"], ["dos","deux","two"] ];
   // collect all the words for each number, in every language, in a single, flat list
   var allWords = [0,1,2].
      concatMap(function(index) {
         return spanishFrenchEnglishWords[index];
      });

   return JSON.stringify(allWords) === '["cero","rien","zero","uno","un","one","dos","deux","two"]';
*/

Related

  1. concatMap(f)
    'use strict';
    let j = require('junify');
    Array.prototype.concatMap = function (f) {
      return Array.prototype.concat.apply([], this.map(f));
    };
    j.matchesAny = function (patterns) {
      return (it) => patterns.some(pattern => j.unify(pattern, it));
    };
    j.match = function (arg, cases) {
    ...
    
  2. concatMap(fn)
    Array.prototype.concatMap = function(fn) {
        return this
            .map((item, index, arr) => fn(index))
            .concatAll();
    };
    
  3. concatMap(modifierFunction)
    Array.prototype.concatMap = function (modifierFunction) {
        return this.map(modifierFunction).concatAll();
    
  4. concatMap(projectionFunction)
    Array.prototype.concatMap = function(projectionFunction){
      return this.map(function(item){
        return projectionFunction(item);
      }).concatAll();
    
  5. concatMap(projectionFunctionThatReturnsArray)
    Array.prototype.concatMap = function (projectionFunctionThatReturnsArray) {
      return this.
        map(function (item) {
          return projectionFunctionThatReturnsArray(item);
        }).
        concatAll();
    };
    
  6. concatMap(projectionFunctionThatReturnsArray)
    Array.prototype.concatMap = function(projectionFunctionThatReturnsArray) {
      return this.
        map(function(item) {
          return projectionFunctionThatReturnsArray(item);
        }).
        concatAll();
    };