Last active
June 5, 2019 13:33
-
-
Save AndersDJohnson/4249568 to your computer and use it in GitHub Desktop.
Object functional js
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
| // Like underscore.js "filter", but returns an object with the keys and values | |
| // instead of just an array of values. | |
| Object.prototype.filter = function(obj, predicate) { | |
| var result = {}, key; | |
| for (key in obj) { | |
| if (obj.hasOwnProperty(key) && predicate(obj[key], key, obj)) { | |
| result[key] = obj[key]; | |
| } | |
| } | |
| return result; | |
| }; |
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
| // Like underscore.js "map", but returns an object with the keys and mapped values | |
| // instead of just an array of mapped values. | |
| Object.prototype.map = function(obj, fn) { | |
| var result = {}, key; | |
| for (key in obj) { | |
| if (obj.hasOwnProperty(key)) { | |
| result[key] = fn(obj[key], key, obj); | |
| } | |
| } | |
| return result; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment