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
?
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
?
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'