Created
March 7, 2014 19:18
-
-
Save hekike/9417978 to your computer and use it in GitHub Desktop.
Reduce
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
/* | |
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