js-mapcat

Map over collections and return one level deep flattened collection

Usage no npm install needed!

<script type="module">
  import jsMapcat from 'https://cdn.skypack.dev/js-mapcat';
</script>

README

js-mapcat

Map over collections and return one level deep flattened collection

Installation

$ npm i js-mapcat

Usage

import mapcat from 'js-mapcat';

mapcat((x) => x * x, [1, 2]); // [1, 4]
mapcat(([x, y, z]) => x * y * z, [1, 2], [3, 4], [5, 6]); // [15, 48]
mapcat(([{a}, {b}]) => a * b, [{ a: 1 }, { a: 2 }], [{ b: 3 }, { b: 4 }]); // [3, 8]

Better example

mapcat with a single collection

Fetch multiple news feeds and produce an array of authors for every article in each feed.

Promise.all([
  fetch('feed/1'),
  fetch('feed/2'),
  fetch('feed/3')
])
.then((feeds) => {

  return mapcat((feed) => {

    return feed.map(({ author }) => author);
  }, feeds);
});