最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - get key corresponding to value in js object - Stack Overflow

programmeradmin4浏览0评论

I have an object such as :

var inspectionMapping = {'a_pillar_lh' : "A Pillar LH",
                       'b_pillar_lh'  : "B Pillar LH"};

console.log(inspectionMapping['a_pillar_lh']);
// Which gives me correct "A Pillar LH".

I am able to get the value corresponding to key.

But I am not able to get the key corresponding to value in javascript Object.

Such as I have "A Pillar LH" and I need to get the corresponding key which is a_pillar_lh.

I have tried to use indexOf but no success.

I have an object such as :

var inspectionMapping = {'a_pillar_lh' : "A Pillar LH",
                       'b_pillar_lh'  : "B Pillar LH"};

console.log(inspectionMapping['a_pillar_lh']);
// Which gives me correct "A Pillar LH".

I am able to get the value corresponding to key.

But I am not able to get the key corresponding to value in javascript Object.

Such as I have "A Pillar LH" and I need to get the corresponding key which is a_pillar_lh.

I have tried to use indexOf but no success.

Share Improve this question edited Apr 6, 2016 at 11:22 digglemister 1,4873 gold badges17 silver badges31 bronze badges asked Apr 6, 2016 at 10:40 RISHABH BANSALRISHABH BANSAL 1401 gold badge2 silver badges9 bronze badges 4
  • Something like Object.keys(obj) – Rayon Commented Apr 6, 2016 at 10:43
  • You have to loop through the keys, checking if the value matches your search. – Poul Bak Commented Apr 6, 2016 at 10:44
  • jsfiddle/rayon_1990/n0y75guq – Rayon Commented Apr 6, 2016 at 10:51
  • @RayonDabre : Jsfiddle example works for me. Thanks buddy :) – RISHABH BANSAL Commented Apr 6, 2016 at 11:04
Add a ment  | 

3 Answers 3

Reset to default 11

You can simply fetch all the keys using Object.keys() and then use .find() function to get the key out from that array, and then nicely wrap it in a function to make it modular.

var inspectionMapping = {
  'a_pillar_lh': "A Pillar LH",
  'b_pillar_lh': "B Pillar LH"
};

Object.prototype.getKey = function(value) {
  var object = this;
  return Object.keys(object).find(key => object[key] === value);
};


alert(inspectionMapping.getKey("A Pillar LH"));

EDIT For those who don't want to use ES6

Object.prototype.getKey = function(value) {
      var object = this;
      for(var key in object){
         if(object[key]==value) return key;
      }
};

Try this.

Object.keys(inspectionMapping).find(key => inspectionMapping[key] === 'A Pillar LH');

If anyone is interested in a Lodash solution to this, here's one I monly use. This method inverts the object, swapping keys for values, and then finds the key by looking up the value against this new object.

var inspectionMapping = {
	'a_pillar_lh': "A Pillar LH",
	'b_pillar_lh': "B Pillar LH"
};

var getKey = function( obj, value ) {
	var inverse = _.invert( obj );
	return _.get( inverse, value, false );
};

alert( 'The key for "A Pillar LH" is "' + getKey( inspectionMapping, 'A Pillar LH' ) + '"' );
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

发布评论

评论列表(0)

  1. 暂无评论