obreezy
obreezy

Reputation: 333

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.

Upvotes: 3

Views: 2250

Answers (1)

Alexander O'Mara
Alexander O'Mara

Reputation: 60527

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);

Upvotes: 6

Related Questions