简体   繁体   English

使用键匹配值

[英]Matching Values with the keys

这是我的自定义Object for comparsion ..从响应我得到的value ,现在我必须将值与此对象中的keys匹配并返回key

That could work for you: 这可能对你有用:

for(key in heartRateRegular)
   if (key == value)
       return key;

The for..in clause iterates through all of the keys of the object. for..in子句遍历对象的所有键。 That's if you want to check the value you get against the Keys of your object. 如果你想检查对象的Keys的值,那就是这样。 If you want to check the value against the Values of your object, do the following: 如果要根据对象的Values检查值,请执行以下操作:

for(key in heartRateRegular)
   if (heartRateRegular[key] == value)
       return key;

Would this work for you? 这对你有用吗?

function getValueFromKey(key) {
    for (key in heartRateRegular) {
        if (heartRateRegular[key] == response) return key;
    }
}

var heartRateRegular = {
    I: 'Irregular',
    Ii: 'Irregularly Irregular',
    Ir: 'Irregularly Regular',
    R: 'Regular',
    Ri: 'Regularly Irregular'
}

response = 'Irregularly Irregular';
alert(getValueFromKey(response));
//alerts 'Ii'

Demo - http://jsfiddle.net/WydQh/ 演示 - http://jsfiddle.net/WydQh/

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

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