Nodejs Array Concatenate Map concatMap(projectionFunctionThatReturnsArray)

Here you can find the source of concatMap(projectionFunctionThatReturnsArray)

Method Source Code

/*/*from w w  w.j a v a 2 s.c  o m*/
Exercise 13: Implement concatMap()

Nearly every time we flatten a tree we chain map() and concatAll(). Sometimes,
 if we're dealing with a tree several levels deep, we'll repeat this
 combination many times in our code. To save on typing, let's create a
 concatMap function that's just a map operation, followed by a concatAll.
*/

Array.prototype.concatMap = function(projectionFunctionThatReturnsArray) {
   return this.
      map(function(item) {
         return projectionFunctionThatReturnsArray(item);
      }).
      // 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(fn)
    Array.prototype.concatMap = function(fn) {
        return this
            .map((item, index, arr) => fn(index))
            .concatAll();
    };
    
  2. concatMap(modifierFunction)
    Array.prototype.concatMap = function (modifierFunction) {
        return this.map(modifierFunction).concatAll();
    
  3. concatMap(projectionFunction)
    Array.prototype.concatMap = function(projectionFunction){
      return this.map(function(item){
        return projectionFunction(item);
      }).concatAll();
    
  4. concatMap(projectionFunctionThatReturnsArray)
    Array.prototype.concatMap = function (projectionFunctionThatReturnsArray) {
      return this.
        map(function (item) {
          return projectionFunctionThatReturnsArray(item);
        }).
        concatAll();
    };
    
  5. concatMap(projectionFunctionThatReturnsArray)
    Array.prototype.concatMap = function(projectionFunctionThatReturnsArray) {
      return this.
        map(function(item) {
          return this.map((i) => {
            return i;
          }).concatAll();
        }).
        concatAll();
    };
    ...