-
-
Save Dviejopomata/31f33690001d79c619ab to your computer and use it in GitHub Desktop.
reduce() vs Universe
This file contains 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
// reduce() awesomeness | |
var a = [1, 2, 3, 4, 5, 5, 3]; | |
var b = [{key: 1, value: 'a'}, {key: 2, value: 'b'}]; | |
var c = [promise1, promise2, promise3...]; | |
// Max | |
a.reduce((x, y) => x > y ? x : y); // 5 | |
// Min | |
a.reduce((x, y) => x < y ? x : y); // 1 | |
// Unique. | |
a.reduce((x, y) => x.indexOf(y) === -1 ? x.concat(y) : x, []); // [1, 2, 3, 4, 5] | |
// Collection to Object | |
b.reduce((o, v) => (o[v['key']] = v['value'], o), {}); // Object {1: "a", 2: "b"} | |
// Promise chaining | |
c.reduce((cur, next) => cur.then(next), Promise.resolve()); // Promise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment