_.pickBy

Creates an object composed of the object properties predicate returns truthy for.

  1. var object = { 'a': 1, 'b': null, 'c': 3, 'd': false, 'e': undefined };
  2.  
  3. // Underscore/Lodash
  4. var result = _.pickBy(object);
  5. console.log(result)
  6. // output: {a: 1, c: 3}
  7.  
  8. // Native
  9. function pickBy(object) {
  10. const obj = {};
  11. for (const key in object) {
  12. if (object[key] !== null && object[key] !== false && object[key] !== undefined) {
  13. obj[key] = object[key];
  14. }
  15. }
  16. return obj;
  17. }
  18. var result = pickBy(object);
  19. console.log(result)
  20. // output: {a: 1, c: 3}

Browser Support

ChromeEdgeFirefoxIEOperaSafari
6.0 ✔