javascript 如何使用其值获取键名(在散列中)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9710315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get a key name (in a hash) by using its value?
提问by james emanon
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 哈希值相匹配。
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”?
采纳答案by JaredPar
In order to get the keyswhich 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;
}
回答by Igor Drozdov
Using underscore.js:
使用 underscore.js:
_.invert(someHash)[value]
回答by rjz
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'));
回答by ShankarSangoli
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;
}
});
回答by Joe
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
确保 hash 具有不是原型的属性。
break
stops the loop once a key is found.
break
一旦找到一个键就停止循环。
回答by Niet the Dark Absol
Perhaps array_flip
could help you. 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.
或许array_flip
可以帮到你。这将创建一个新数组,其中键是值,反之亦然。然后,只需在该新数组中查找键,结果值就是您要查找的原始数组中的键。
回答by whiteatom
Loop through the object.
循环遍历对象。
function findInObj(obj, needle)
for(var x in obj){
if(obj.x==needle) return x;
}
return false;
}
回答by kennebec
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 */