runtimeZero
runtimeZero

Reputation: 28126

Filtering an object based on its values using lodash

How would I use lodash to get the following output ?

input = {
'property1' : true,
'property2' : false,
'property3' : false
}
// only pick those properties that have a false value
output = ['property2', 'property3'];

I currently have the following in place:

var output = [];
_.forEach(input, (value, key) => {
  if (value === false) {
   output.push(key);  
  }
});

Upvotes: 1

Views: 650

Answers (2)

JesusTinoco
JesusTinoco

Reputation: 11828

You can do something like that using lodash:

const output = _.keys(_.pickBy(input, function(value, key) {return !value }));

Upvotes: 1

tymeJV
tymeJV

Reputation: 104795

Plain JS you can do:

var output = Object.keys(input).filter(function(k) { return input[k] === false })

Upvotes: 2

Related Questions