Created
July 11, 2014 10:45
-
-
Save noel9999/c53cae062974c44d897c to your computer and use it in GitHub Desktop.
Javascript: 練習實作select,模仿ruby的select
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
Array.prototype.select = function(callback){ | |
count = this.length; | |
results = new Array(); | |
for(i=0;i<count;i++){ | |
if(callback(this[i])) | |
results.push(this[i]); | |
} | |
return results; | |
}; | |
gg = [1,2,3,4,5,6,7,8,9] | |
//[1, 2, 3, 4, 5, 6, 7, 8, 9] | |
gg.select(function(i){return i%2==0}); | |
//[2, 4, 6, 8] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment