Find properties of JS object with right values โ€‹โ€‹using lodash

Let's say I have an object like:

var foo = { alpha: true, beta: false, gamma: true } 

I can use _.findKey to get one key with a true value, but I would really like to get an array containing all the keys with a true value. For example.

 _.findAllKeys(foo, function(val) { return val; }); // yields -> ["alpha", "gamma"] 

Itโ€™s enough just to write a function to do this, but it seems like the obvious generalization of findKey that I feel is, I just have to skip it. Does lodash have such a function?

+8
javascript lodash
source share
7 answers

 var foo = { alpha: true, beta: false, gamma: true }; var t1 = _.keys(_.pick(foo, _.identity)); console.log(t1); var t2 = _(foo).pick(_.identity).keys().value(); console.log(t2); 
 <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.min.js"></script> <script src="https://getfirebug.com/firebug-lite-debug.js"></script> 

Edit:
As noted in @backdesk, _.pick no longer works for lodash 4 because _.pickBy was split.

 var foo = { alpha: true, beta: false, gamma: true }; var t1 = _.keys(_.pickBy(foo, _.identity)); console.log(t1); var t2 = _(foo).pickBy(_.identity).keys().value(); console.log(t2); // _.pickBy defaults to _.identity var t3 = _.keys(_.pickBy(foo)); console.log(t3); var t4 = _(foo).pickBy().keys().value(); console.log(t4); 
 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.8.2/lodash.min.js"></script> <script src="https://getfirebug.com/firebug-lite-debug.js"></script> 
+16
source share

I found an answer that both feels silly and elegant.

 var foo = { alpha: true, beta: false, gamma: true }; _.invert(foo, true).true // yields -> ["alpha", "gamma"] 
+6
source share

pickBy in loDash uses _.identity by default to filter properties so you can use it like this:

 _.pickBy({'a': undefined, 'b':1, c:{}}); // => Object {b: 1, c: Object} 
+2
source share

I think you are looking for a pick method.

Return a copy of the object, filter to have only values โ€‹โ€‹for white keys (or an array of valid keys). Alternatively, it takes a predicate indicating which keys to select.

 var foo = { alpha: true, beta: false, gamma: true }; var picked = _.pick(foo, function(value) { return value; }); console.log(picked); $('#output').html(JSON.stringify(picked)); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.js"></script> <div id="output"> </div> 
+1
source share

I personally prefer the following - although this is more detailed, because I think it is more obvious what he is doing. This requires the es6 syntax:

 _.toPairs(foo) .filter([key, value] => value) .map([key, value] => key); 

If your ESLint does not allow the use of unused variables, you can, for example, use the following in .eslint.yml :

 rules: no-unused-vars: - 2 - vars: all args: after-used argsIgnorePattern: _$ 

What allows you to write

 _.toPairs(foo) .filter([key_, value] => value) .map([key, value_] => key); 
0
source share

Just try

 var foo = { alpha: true, beta: false, gamma: true } 

_. pickBy (foo, _.identity);

0
source share

If you do not want to write and duplicate it, rather than import the entire utility library, you can also use this open-source resource object-clean .

Here's how it works:

 function clean(obj: {[any]: any}): {[any]: any} { const newObj = {}; forEach(obj, (val, key) => { if (!val) return; newObj[key] = val; }); return newObj; }; 

Example:

 clean({ foo: null, bar: 'foo' }) // => { bar: 'foo' } 
0
source share

All Articles