Skip to content

Instantly share code, notes, and snippets.

@matthew-gerstman
Created March 18, 2019 22:14
Show Gist options
  • Save matthew-gerstman/31a1fc7ad9ef71acd0d9ed5ee3503e90 to your computer and use it in GitHub Desktop.
Save matthew-gerstman/31a1fc7ad9ef71acd0d9ed5ee3503e90 to your computer and use it in GitHub Desktop.
function flatMap(list, func) {
return list.reduce((acc, item) => {
const mappedItem = func(item);
if (Array.isArray(mappedItem)) {
acc = acc.concat(mappedItem);
} else {
acc.push(mappedItem);
}
return acc;
}, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment