adding an initial implementation of a 'pick' function. #523

This commit is contained in:
Jeremy Ashkenas
2012-04-02 15:01:26 -04:00
parent 4fa0a9f4d5
commit 719cd24aa9
3 changed files with 18 additions and 20 deletions

View File

@@ -648,14 +648,13 @@
return obj;
};
// Restrict a given object to the properties named
_.restrict = function(obj) {
if (obj !== Object(obj)) throw new TypeError('Invalid object');
var dest = {};
each(_.flatten(slice.call(arguments, 1)), function(prop) {
if (prop in obj) dest[prop] = obj[prop];
// Return a copy of the object only containing the whitelisted properties.
_.pick = function(obj) {
var result = {};
each(_.flatten(slice.call(arguments, 1)), function(key) {
if (key in obj) result[key] = obj[key];
});
return dest;
return result;
};
// Fill in a given object with default properties.