3

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.

2
  • 2
    Object.keys and Array#filter should be enough Commented Dec 21, 2016 at 3:29
  • Are you looking for a specific value...or just any that have a match? Commented Dec 21, 2016 at 3:32

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.