This is code from an episode of Fun Fun Function: https://youtu.be/6sQDTgOqh-I
Last active
May 29, 2020 02:52
-
-
Save mpj/8256bdfae3168733a0bf733c604142d0 to your computer and use it in GitHub Desktop.
Arrow functions
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
const dragonEvents = [ | |
{ type: 'attack', value: 12, target: 'player-dorkman' }, | |
{ type: 'yawn', value: 40 }, | |
{ type: 'eat', target: 'horse' }, | |
{ type: 'attack', value: 23, target: 'player-fluffykins' }, | |
{ type: 'attack', value: 12, target: 'player-dorkman' }, | |
] |
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
const totalDamageOnDorkman = dragonEvents | |
.filter(function (event) { | |
return event.type === 'attack' | |
}) | |
.filter(function (event) { | |
return event.target === 'player-dorkman' | |
}) | |
.map(function(event) { | |
return event.value | |
}) | |
.reduce(function(prev, value) { | |
return (prev || 0) + value | |
}) |
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
const totalDamageOnDorkman = dragonEvents | |
.filter(e => e.type === 'attack') | |
.filter(e => e.target === 'player-dorkman') | |
.map(e => e.value) | |
.reduce((prev, x) => (prev || 0) + x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why .reduce(summerInitialValue) does not work?