-
-
Save gerardpaapu/5086585 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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