简体   繁体   English

如何使用其值获取密钥名称(在哈希中)?

[英]How can I get a key name (in a hash) by using its value?

I understand this is a little unorthodox. 我明白这有点不正统。

Lets say I have this hash. 让我说我有这个哈希。

 someHash = {
    'item1' => '5',
    'item2' => '7',
    'item3' => '45',
    'item4' => '09'
}

Using native js, or prototype or Jquery -- is there a method that will enable me to get the "key name" by just having the value? 使用原生js,原型或Jquery - 是否有一种方法可以让我通过获得值得到“关键名称”?

I don't want all the keys, just the one that matches my value. 我不想要所有的钥匙,只需要与我的价值相匹配的钥匙。 Sorta of a like a map in reverse? 像一张地图反过来排序?

I am getting a return from the db which I get a "value" and I have to match that value with some js hash on the front end. 我从db得到一个返回值,我得到一个“值”,我必须将该值与前端的一些js hash匹配。

So the app hands me "45"... Is there a way to use js (prototype or jquery) to then get the key "item3"? 所以应用程序递给我“45”...有没有办法使用js(原型或jquery)然后获得键“item3”?

使用underscore.js:

_.invert(someHash)[value]

In order to get the keys which map to a given value you'll need to search the object properties. 为了获取映射到给定值的 ,您需要搜索对象属性。 For example 例如

function getKeysForValue(obj, value) {
  var all = [];
  for (var name in obj) {
    if (Object.hasOwnProperty(name) && obj[name] === value) {
      all.push(name);
    }
  }
  return all;
}

There is no direct method but you can try something like this. 没有直接的方法,但你可以尝试这样的事情。

var key;
$.each(someHash, function(key, val){
    if(val == 'valToCompare'){
        key = key;
        return false;
    }
});

Without uniqueness you can get the first: 没有唯一性,你可以得到第一个:

var hash = {item1: 0, item2: 1},
    value = 1,
    key;
for(var i in hash)
{
    if (hash.hasOwnProperty(i) && hash[i] === value) {
        key = i;
        break;
    }
}
key; // item2

hasOwnProperty ensures that hash has the property not a prototype. hasOwnProperty确保哈希具有属性而不是原型。

break stops the loop once a key is found. 一旦找到密钥, break停止循环。

I don't know of a native answer, but you could write your own: 我不知道原生答案,但你可以写自己的:

var someHash = {
  'item1' : '5',
  'item2' : '7',
  'item3' : '45',
  'item4' : '09'
};

function getKeyByValue(hash, value) {
  var key;
  for(key in hash) {
    if (hash[key] == value) return key;
  }
}

alert(getKeyByValue(someHash, '7'));

Perhaps array_flip could help you. 也许array_flip可以帮到你。 This makes a new array where the keys are the values and vice versa. 这使得一个新的数组中的键是值,反之亦然。 Then, just look for the key in that new array, and the resulting value is the key in the original array you were looking for. 然后,只需查找该新数组中的键,结果值就是您要查找的原始数组中的键。

Loop through the object. 循环通过对象。

function findInObj(obj, needle)
    for(var x in obj){
      if(obj.x==needle) return x;
    }
    return false;
}

Since more than one property in an object can have the same value, you could return an array of the property names that match the value. 由于对象中的多个属性可以具有相同的值,因此可以返回与该值匹配的属性名称数组。

var someHash={
    'item1':'5',
    'item2':'7',
    'item3':'45',
    'item4':'09',
    'item5':'7'
}
function hasAny(what){
    var names= [];
    for(var p in this){
        if(this[p]=== what) names.push(p);
    }
    return names;
}

hasAny.call(someHash, '7'); hasAny.call(someHash,'7');

/* returned value: (Array) item2,item5 */ / *返回值:(数组)item2,item5 * /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM