Javascript Array concatAll()

Description

Javascript Array concatAll()


Array.prototype.concatAll = function() {
  var results = [];
  this.forEach(subArray =>/*www .  j a  v a2 s.com*/
    subArray.forEach(item => results.push(item))
  );
  return results;
};

console.log(JSON.stringify([ [1,2,3], [4,5,6], [7,8,9] ].concatAll()) === "[1,2,3,4,5,6,7,8,9]");
// [1,2,3].concatAll(); // throws an error because this is a one-dimensional array

Javascript Array concatAll()


Array.prototype.concatAll = function () {
  var results = []
  this.forEach(function(subArray) {
    results.push.apply(results, subArray)
  })//w  w  w  .j  a  va 2  s  . c  om

  return results
}

Array.prototype.concatMap = function (projectionFunctionThatReturnsArray) {
  return this.
    map(function (item) {
      return projectionFunctionThatReturnsArray(item);
    }).
    concatAll();
};

Javascript Array concatAll()


/**//from  w  w  w . ja v a  2s .  co  m
 * @description concatAll() aka flattenDeep(), based on http://reactivex.io/learnrx/
 * @return {array} concatenated array.
 */

Array.prototype.concatAll = function () {
  let results = [];
  this.forEach(function (subArray) {
    results.push.apply(results, subArray);
  });
  return results;
};

Javascript Array concatAll()


Array.prototype.concatAll = function() {
 var results = [];
 this.forEach(function(subArray) {
  subArray.forEach((i) => {//ww w .ja v a2 s .  c o m
      results.push(i);
    });
 });

 return results;
};

// JSON.stringify([ [1,2,3], [4,5,6], [7,8,9] ].concatAll()) === "[1,2,3,4,5,6,7,8,9]"
// [1,2,3].concatAll(); // throws an error because this is a one-dimensional array

Javascript Array concatAll()


Array.prototype.concatAll = function() {
  return this.reduce((results, current) => {
    if(Array.isArray(current)) {
        return results.concat(current).concatAll();
    } else {/*from w w  w  .  j a  v a 2  s . co m*/
        return (results.push(current), results);
    }
  }, []);
};

Javascript Array concatAll()


/*/*from w ww  . ja v a 2 s  . c o  m*/
Let's add a concatAll() function to the Array type. The concatAll() function
 iterates over each sub-array in the array and collects the results in a new,
  flat array. Notice that the concatAll() function expects each item in the
   array to be another array.
*/

Array.prototype.concatAll = function() {
 var results = [];
 this.forEach(function(subArray) {
  results.push.apply(results, subArray);
 });

 return results;
};

// JSON.stringify([ [1,2,3], [4,5,6], [7,8,9] ].concatAll()) === "[1,2,3,4,5,6,7,8,9]"
// [1,2,3].concatAll(); // throws an error because this is a one-dimensional array

Javascript Array concatAll()


Array.prototype.concatAll = function() {
 var results = [];
 this.forEach(subArray =>/*from  w w w .  ja  v  a 2 s  .c  om*/
    subArray.forEach(item => results.push(item))
 );
 return results;
};

Array.prototype.concatMap = function(projectionFunctionThatReturnsArray) {
 return this
  .map(item => projectionFunctionThatReturnsArray(item))
  .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(index => spanishFrenchEnglishWords[index]);

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



PreviousNext

Related