Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6627641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:02:16+00:00 2026-05-25T22:02:16+00:00

This must be a duplicate, but I’ve been Googling retrieve by value from object

  • 0

This must be a duplicate, but I’ve been Googling “retrieve by value from object javascript” and “javascript lookup object by value” and every variant and got nowhere, so apologies and here goes.

Say I have a JavaScript object like this:

var options = {"ford": "red", "citroen": "blue"};

How do I do look up value blue to get citroen back?

There’s always the ‘write your own function’ route, I guess:

function returnbyValue(options, v):
    for (var prop in options) {
      if (options.hasOwnProperty(v)) {
        if (options[prop] === v) {
            return prop;
        }
      }
    }
    return null;

but does JavaScript have anything inbuilt, or is there a neater way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:02:17+00:00Added an answer on May 25, 2026 at 10:02 pm

    The property of an object can be accessed just like an associative array!
    This worked like a charm!

    var obj = {
        'key': 'val'
    };
    
    alert( obj['key'] );
    

    Alternatively, if you wish to use a method you can create a prototype method.

    Object.prototype.getPropertyByString = function( str ) {
        return this[str];
    };
    
    alert( obj.getPropertyByString( 'key' ) );
    

    Edit: Wow I just noticed I failed to answer your question, my apologies! Allow me to get a second chance.

    There is no built in function, but my script below works!

    var obj = {
        'key': 'val'
    };
    
    Object.prototype.getKeyByValue = function( object ) {
        for(var key in this) {
            if(this.key === object) {
                return key;
            }
        }
    
        return null;
    };
    
    alert( obj.getKeyByValue( 'val' ) );
    

    It loops through the object and returns the key if it matches a value. This wil work, no matter if the value is an int, string, object, anything. This is because I’ve used the strict equal comparison (“===”) which also checks if the object type is the same.

    Also, please note that checking if the property exists is silly if you’re looping through all keys of the object anyway. Obviously, when you’re looping through all keys, they exist.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This must be a FAQ, but I can’t find a duplicate question! There are
OK, this must be a duplicate question, but I cannot find the answer: I
I must apologize if this is a duplicate question, but I can't seem to
This must, must, must be a duplicate but I can't find the answer either
This must be a classic .NET question for anyone migrating from Java. .NET does
This must be a very simple question, but I don't seem to be able
This must be a stupid question, but nevertheless I find it curious: Say I
This must be really obvious, but how do I make a winsock listen to
This must be simple, but I can't seem to figure it out. I am
This must be a dumb question, but I don't see any obvious references. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.