简体   繁体   中英

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. That's if you want to check the value you get against the Keys of your object. If you want to check the value against the Values of your object, do the following:

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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