Skip to content

Instantly share code, notes, and snippets.

@Dviejopomata
Forked from kutyel/reduce.js
Last active February 23, 2016 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dviejopomata/31f33690001d79c619ab to your computer and use it in GitHub Desktop.
Save Dviejopomata/31f33690001d79c619ab to your computer and use it in GitHub Desktop.
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