Created
September 27, 2015 02:19
-
-
Save dnasca/a3aa602cedcbcf04a911 to your computer and use it in GitHub Desktop.
functional filtering > imperative iteration
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
//stop imperatively iterating! | |
var fictionalCharacters = [ | |
{ name: 'Beavis', universe: 'Beavis and Butthead'}, | |
{ name: 'Cartman', universe: 'Southpark'}, | |
{ name: 'Stimpy', universe: 'Ren and Stimpy'}, | |
{ name: 'Butthead', universe: 'Beavis and Butthead'}, | |
{ name: 'Stan', universe: 'Southpark'}, | |
{ name: 'Ren', universe: 'Ren and Stimpy'}, | |
{ name: 'Ironman', universe: 'Marvel'} | |
] | |
//the imperative iteration | |
var southpark = [] | |
for (var i = 0; i < fictionalCharacters.length; i++) { | |
if (fictionalCharacters[i].universe === 'southpark') { | |
southpark.push(fictionalCharacters[i]) | |
} | |
} | |
//the functional filter | |
var beavisAndButthead = fictionalCharacters.filter(function(fictionalCharacters)) { | |
return fictionalCharacters.universe === 'Beavis and Butthead' | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment