Skip to content

Instantly share code, notes, and snippets.

@Dviejopomata Dviejopomata/reduce.js forked from kutyel/reduce.js
Last active Feb 23, 2016

Embed
What would you like to do?
reduce() vs Universe
// 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
You can’t perform that action at this time.