Robert.Cutright's implementation was great. (underscore.js has this function called omit
http://underscorejs.org/#omit)
I forked his code and modified two parts.
First, I reused _.pick
.
Second, I use reduce
instead of assing
and filter
. Because reduce
is more useful and simple solution to get result that I want.
var _ = {}; // Joeun's Pick implementation (GREAT JOB!) _.pick = (target, ...keys) => { if (typeof keys[0] == 'function') { var predicate = keys[0]; keys = Object.keys(target); return keys.reduce((obj, key) => { return predicate(target[key], key, target) ? (obj[key] = target[key], obj) : obj; }, {}) } return keys.reduce((obj, key) => { return obj[key] = target[key], obj; }, {}); }; // Robert.Cutright's implementation of the inverse of pick (called flick); // In other words, you supply the keys you want to throw away. _.flick = (target, ...keys) => { if (typeof keys[0] == 'function') { var predicate = keys[0]; keys = Object.keys(target); return keys.reduce((obj, key) => { return predicate(target[key], key, target) ? obj : (obj[key] = target[key], obj); }, {}) } var obj = Object.assign({}, target); Object.keys(obj).filter(key => keys.includes(key) ? delete obj[key] : obj[key]); return obj; }; // Joeun's flick (called flick2) _.flick2 = (target, ...keys) => { if (typeof keys[0] == 'function') { var predicate = keys[0]; return _.pick(target, (...args) => !predicate(...args)) // first part } var target_keys = Object.keys(target); // second part return target_keys.reduce((obj, key) => { return keys.includes(key) ? obj : (obj[key] = target[key], obj); }, {}); };
- var _ = {};
- // Joeun's Pick implementation (GREAT JOB!)
- _.pick = (target, ...keys) => {
- if (typeof keys[0] == 'function') {
- var predicate = keys[0];
- keys = Object.keys(target);
- return keys.reduce((obj, key) => {
- return predicate(target[key], key, target) ? (obj[key] = target[key], obj) : obj;
- }, {})
- }
- return keys.reduce((obj, key) => {
- return obj[key] = target[key], obj;
- }, {});
- };
- // Robert.Cutright's implementation of the inverse of pick (called flick);
- // In other words, you supply the keys you want to throw away.
- _.flick = (target, ...keys) => {
- if (typeof keys[0] == 'function') {
- var predicate = keys[0];
- keys = Object.keys(target);
- return keys.reduce((obj, key) => {
- return predicate(target[key], key, target) ? obj : (obj[key] = target[key], obj);
- }, {})
- }
- var obj = Object.assign({}, target);
- Object.keys(obj).filter(key => keys.includes(key) ? delete obj[key] : obj[key]);
- return obj;
- };
- // Joeun's flick (called flick2)
- _.flick2 = (target, ...keys) => {
- if (typeof keys[0] == 'function') {
- var predicate = keys[0];
- return _.pick(target, (...args) => !predicate(...args)) // first part
- }
- var target_keys = Object.keys(target); // second part
- return target_keys.reduce((obj, key) => {
- return keys.includes(key) ? obj : (obj[key] = target[key], obj);
- }, {});
- };
var result; var testObj = {name: 'moe', age: 50, userid: 'moe1'}; // _.pick Tests result = _.pick(testObj, 'name', 'age'); Test.assertSimilar(result, {name: 'moe', age: 50}); result = _.pick(testObj, function(value, key, object) { return typeof value == 'number'; }); Test.assertSimilar(result, {age: 50}); // _.flick Tests result = _.flick(testObj, 'name', 'age'); Test.assertSimilar(result, {userid: 'moe1'}); result = _.flick(testObj, function(value, key, object) { return typeof value == 'number'; }); Test.assertSimilar(result, {name: 'moe', userid: 'moe1'}); // _.flick2 Tests result = _.flick2(testObj, 'name', 'age'); Test.assertSimilar(result, {userid: 'moe1'}); result = _.flick2(testObj, function(value, key, object) { return typeof value == 'number'; }); Test.assertSimilar(result, {name: 'moe', userid: 'moe1'});
- var result;
- var testObj = {name: 'moe', age: 50, userid: 'moe1'};
- // _.pick Tests
- result = _.pick(testObj, 'name', 'age');
- Test.assertSimilar(result, {name: 'moe', age: 50});
- result = _.pick(testObj, function(value, key, object) {
- return typeof value == 'number';
- });
- Test.assertSimilar(result, {age: 50});
- // _.flick Tests
- result = _.flick(testObj, 'name', 'age');
- Test.assertSimilar(result, {userid: 'moe1'});
- result = _.flick(testObj, function(value, key, object) {
- return typeof value == 'number';
- });
- Test.assertSimilar(result, {name: 'moe', userid: 'moe1'});
- // _.flick2 Tests
- result = _.flick2(testObj, 'name', 'age');
- Test.assertSimilar(result, {userid: 'moe1'});
- result = _.flick2(testObj, function(value, key, object) {
- return typeof value == 'number';
- });
- Test.assertSimilar(result, {name: 'moe', userid: 'moe1'});
Function 'pick' in the Underscore.js (http://underscorejs.org/#pick)
"Return a copy of the object, filtered to only have values for the whitelisted keys (or array of valid keys). Alternatively accepts a predicate indicating which keys to pick."
_.pick({name: 'moe', age: 50, userid: 'moe1'}, 'name', 'age');
// {name: 'moe', age: 50}
_.pick({name: 'moe', age: 50, userid: 'moe1'}, function(value, key, object) {
return typeof value == 'number';
});
// {age: 50}
var _ = {};
_.pick = (target, ...keys) => {
if (typeof keys[0] == 'function') {
var predicate = keys[0];
keys = Object.keys(target);
return keys.reduce((obj, key) => {
return predicate(target[key], key, target) ? (obj[key] = target[key], obj) : obj;
}, {})
}
return keys.reduce((obj, key) => {
return obj[key] = target[key], obj;
}, {});
};
var result;
result = _.pick({name: 'moe', age: 50, userid: 'moe1'}, 'name', 'age');
Test.assertSimilar(result, {name: 'moe', age: 50});
result = _.pick({name: 'moe', age: 50, userid: 'moe1'}, function(value, key, object) {
return typeof value == 'number';
});
Test.assertSimilar(result, {age: 50});