Javascript Array concatMap(callback)
Array.prototype.concatMap = function (callback) { const newArray = [];//from w ww .j ava2 s. c om this.map(callback).forEach((element) => { element.forEach((element) => { newArray.push(element); }); }); return newArray; };
'use strict';/* w w w .ja va 2 s . c o m*/ Array.prototype.concatMap = function (cb) { return this.map(cb).concatAll() }
/**//from w w w . java2s . c o m * Function does the same behavior as map and concatAll in action. * @param fn => fn is a callback. */ Array.prototype.concatMap = function(fn) { return this .map((item, index, arr) => fn(index)) .concatAll(); };
Array.prototype.concatMap = function(projectionFunction) { return this.map(function(item) { return projectionFunction(item); })./*from ww w.jav a 2 s. c om*/ concatAll(); }; Array.prototype.map = function(callback) { var result = []; this.forEach(function(item) { result.push(callback(item)); }); return result; }; //concatAll() only works on singly nested arrays //for example: [["a", ["test"]]] returns ["a", >Array[1]] Array.prototype.concatAll = function(array) { var results = []; this.forEach(function(subArray) { if (Array.isArray(subArray)) { subArray.forEach(function(item) { results.push(item); }); } else { results.push(item); } }); return results; }; var test = [["Build"], ["a"], ["bird's"], ["nest"], ["to"], ["make", "a"], ["test"]]; test.concatMap(function(item) { return item; });