Last active
November 14, 2016 12:49
-
-
Save a-eid/433caced2b3cd5df57a87fd10a674a38 to your computer and use it in GitHub Desktop.
pure reducer with map && filter (not complete)
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
let array = [1,2,3,4] | |
function reducer(state , action){ | |
switch(action.type){ | |
case "remove": | |
return state.filter(function(elem , index){ | |
return index != action.id | |
}) | |
case 'inc': | |
return state.map(function(elem , index){ | |
return (index == action.id) ? elem + 1 : elem | |
}) | |
} | |
} | |
action0 = { | |
type : 'remove', | |
id : 0 | |
} | |
action1 = { | |
type : 'remove', | |
id : 1 | |
} | |
action2 = { | |
type : 'inc', | |
id : 2 | |
} | |
action3 = { | |
type : 'inc', | |
id : 3 | |
} | |
action0 = { | |
type : 'inc', | |
id : 0 | |
} | |
action1 = { | |
type : 'inc', | |
id : 1 | |
} | |
action2 = { | |
type : 'inc', | |
id : 2 | |
} | |
action3 = { | |
type : 'remove', | |
id : 3 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment