Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return JavaScript object keys that share the same value without knowing key name?

Say I have a simple JavaScript object:

 {"omar":"espn.com","omar1":"espn1.com","omar3":"espn.com"}

How do I return all keys that share "espn.com" without knowing the name of the keys?

In this case, only "omar" and "omar3" should be returned.

like image 276
obreezy Avatar asked Mar 28 '26 00:03

obreezy


1 Answers

Just enumerate the properties with Object.keys and Array#filter the ones you want.

Working Example:

var o = {"omar":"espn.com","omar1":"espn1.com","omar3":"espn.com"};

var matched = Object.keys(o).filter(function(key) {
    return o[key] === 'espn.com';
});

console.log(matched);
like image 137
Alexander O'Mara Avatar answered Mar 29 '26 14:03

Alexander O'Mara



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!