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

javascript - Find key for specific value on object in JS - Stack Overflow

programmeradmin7浏览0评论

I have an object as such that has been generated by using the lodash _.zipObject() function. So I have 2 arrays, one of locations, one of numbers.

var locs = {'Aberdeen': 304, 'Aberystwith': 109, 'Belfast': 219, 'Birmingham': 24, 'Brighton': 147, …}

I need to return the key based on an input value. For example, function(304) would return 'Aberdeen'.

I've tried _.findkey(locs, 304); but this just returns undefined. Any other attempt I've tried always returns either undefined or -1. Not really sure where to go from here.

I have an object as such that has been generated by using the lodash _.zipObject() function. So I have 2 arrays, one of locations, one of numbers.

var locs = {'Aberdeen': 304, 'Aberystwith': 109, 'Belfast': 219, 'Birmingham': 24, 'Brighton': 147, …}

I need to return the key based on an input value. For example, function(304) would return 'Aberdeen'.

I've tried _.findkey(locs, 304); but this just returns undefined. Any other attempt I've tried always returns either undefined or -1. Not really sure where to go from here.

Share Improve this question edited Sep 20, 2017 at 11:29 Ori Drori 192k32 gold badges237 silver badges227 bronze badges asked Sep 20, 2017 at 11:26 Sam DriverSam Driver 921 gold badge1 silver badge14 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 5

To find the key use a predicate function with _.findKey():

var locs = {'Aberdeen': 304, 'Aberystwith': 109, 'Belfast': 219, 'Birmingham': 24, 'Brighton': 147 };

var key = _.findKey(locs, function(v) {
  return v === 304;
});

console.log(key);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

You can create the predicate by currying _.eq() with the requested value:

var locs = {'Aberdeen': 304, 'Aberystwith': 109, 'Belfast': 219, 'Birmingham': 24, 'Brighton': 147 };

var key = _.findKey(locs, _.curry(_.eq, 304));

console.log(key);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

With pure Javascript use Object#keys function to get all keys and then compare with your element in the Array#find function

const obj = {'Aberdeen': 304, 'Aberystwith': 109, 'Belfast': 219, 'Birmingham': 24, 'Brighton': 147};

const key = Object.keys(obj).find(key => obj[key] === 304);

console.log(key);

With lodash pass predicate into the function

const key = _.findkey(locs, (value) => value === 304);

You can use custom function to find a key:

function findKey(dataObj, value){

    for(var key in dataObj){
        if(dataObj.hasOwnProperty(key) && dataObj[key] == value){
            return key;
        }
    }

    return false;

}

var locs = {'Aberdeen': 304, 'Aberystwith': 109, 'Belfast': 219, 'Birmingham': 24, 'Brighton': 147, …}

console.log(findKey(locs, 304));

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 locs = {'Aberdeen': 304, 'Aberystwith': 109, 'Belfast': 219, 'Birmingham': 24, 'Brighton': 147, …}    
Object.prototype.getKey = function(value) {    
  var object = this;    
  return Object.keys(object).find(key => object[key] === value);    
};    
alert(locs.getKey(304));

You could do it like this:

var locs = {'Aberdeen': 304, 'Aberystwith': 109, 'Belfast': 219, 'Birmingham': 24, 'Brighton': 147}

const getKeyByValue = (obj, value) => 
        Object.keys(obj).find(key => obj[key] === value);

console.log(getKeyByValue(locs, 304));

发布评论

评论列表(0)

  1. 暂无评论