0

How to find the key from js map array of perticular value? For example:

var map = {'key1': 'value1', 'key2': 'value2'};

How I find the key of value1?

Jaap
  • 81,064
  • 34
  • 182
  • 193
user3614914
  • 31
  • 1
  • 3

1 Answers1

1
Object.prototype.getKeyByValue = function( value ) {
    for( var prop in this ) {
        if( this.hasOwnProperty( prop ) ) {
             if( this[ prop ] === value )
                 return prop;
        }
    }
}

var test = {
   key1: 42,
   key2: 'foo'
};

test.getKeyByValue( 42 );  // returns 'key1'

from: JavaScript object get key by value

Community
  • 1
  • 1
Mohammad Alabed
  • 809
  • 6
  • 17