Skip to content

Instantly share code, notes, and snippets.

@hekike
Created March 7, 2014 19:18
Show Gist options
  • Save hekike/9417978 to your computer and use it in GitHub Desktop.
Save hekike/9417978 to your computer and use it in GitHub Desktop.
Reduce
/*
1.
a: NaN
b: 0123
c: 6
*/
var transactions = [{ id: 1, value: 1 }, { id: 2, value: 2 }, { id: 3, value: 3 }];
var total = transactions.reduce(function(prev, transaction) {
return (prev + parseInt(transaction.value, 10));
}, '0');
console.log(result);
/*
2.
a: [0, 1, 3, 2, 5, 4]
b: [1, 0, 3, 2, 5, 4]
c: [0, 1, 3, 2, 4, 5]
*/
var result = [[0, 1], [2, 3], [4, 5]].reduce(function(a, b) {
return a.concat(b.reverse());
});
console.log(result);
/*
3.
a: [0, 1, 0, 0, 1, 1]
b: [0, 1, 0, 1, 0, 1]
*/
var result = [0, 1].reduce(function(a, b, index, arr) {
return a.concat(arr).concat(b);
}, []);
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment