Skip to content

Instantly share code, notes, and snippets.

@gerardpaapu
Forked from aaronpowell/selectMany.js
Last active September 25, 2019 10:46
Show Gist options
  • Save gerardpaapu/5086585 to your computer and use it in GitHub Desktop.
Save gerardpaapu/5086585 to your computer and use it in GitHub Desktop.
(function () {
var apply = Function.prototype.apply;
var flatten = apply.bind(Array.prototype.concat, []);
Array.prototype.selectMany = function (fn) {
return flatten(this.map(fn));
};
}());
// usage
console.log([[1,2,3], [4,5,6]].selectMany(function (x) { return x; })); //[1,2,3,4,5,6]
console.log([{ a: [1,2,3] }, { a: [4,5,6] }].selectMany(function (x) { return x.a; }));
console.log([{ title: 'Some Blog', tags: ['being awesome'] }, { title: 'Some Other Blog', tags: ['still awesome', 'javascript'] } ].selectMany(function (blog) { return blog.tags; }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment