Created
December 9, 2015 02:16
-
-
Save eternal44/d0ec4e64beaa42a41752 to your computer and use it in GitHub Desktop.
transform (map) function
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
var transform = function(collection, predicate){ | |
if (Array.isArray(collection)){ | |
return collection.map(predicate); | |
} else if (typeof collection === 'string'){ | |
collection = collection.split(''); | |
return collection.map(predicate).join(''); | |
} else if (typeof collection === 'object'){ | |
return Object.keys(collection).reduce(function(results, property){ | |
results[property] = predicate(collection[property]); | |
return results; | |
}, {}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment