Ad
ES2015
Babel
Objects
Data Types

Joeun's implementation of the pick function in the 'underscore' library. I have extended the library with a new function with a clever name. :) Called 'flick' which is the inverse of 'pick'. Some object/booger picking operations. lol

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;
    };
    
    • 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;
    • };