Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return an array of properties that match pattern using lodash

Is there any way using the lodash function to return an array of properties that match to a pattern?

_.magicMethod({a:'hi', b:13, c:undefined, d:null, e:null}, null)

return => `['d','e']`

I checked the docs but I don't found nothing :/ Thank you.

like image 685
user3111903 Avatar asked Dec 30 '25 15:12

user3111903


1 Answers

There's probably not a single function version of it; but you can do:

function magicMethod(obj, value) {
    return _.keys(_.pick(obj, function(propertyValue) {
         return propertyValue === value;
    }));
}

_.pick creates an object with only the properties with values matching the specified value, then _.keys extracts the keys of that object.

like image 132
Retsam Avatar answered Jan 01 '26 03:01

Retsam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!