Ad
ES2015
Babel
Objects
Data Types

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.

Code
Diff
  • 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);
    • }, {});
    • };
ES2015
Babel
Objects
Data Types

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;
  }, {});
};