在jscript中从value中查找属性名称

时间:2012-11-14 21:52:16

标签: javascript jquery

如果我知道定义为

的对象的值,我正在尝试获取属性名称
expOperators = {
    "0": "Select operator",
    "GREATERTHAN": "After",
    "LESSTHAN": "Before",
    "GREATERTHANEQUALTO": "On or After",
    "LESSTHANEQUALTO": "On or Before",
    "EQUALS": "On",
    "BETWEEN": "Between",
    "ISNULL": "Is Null",
    "ISNOTNULL": "Is not Null"
};

显然,如果我需要找到价值,那将很容易,但我需要完成一项相反的任务,知道价值,找出什么是财产。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您将遍历对象并将对象值与您要查找的对象进行比较。

expOperators = {
    "0": "Select operator",
    "GREATERTHAN": "After",
    "LESSTHAN": "Before",
    "GREATERTHANEQUALTO": "On or After",
    "LESSTHANEQUALTO": "On or Before",
    "EQUALS": "On",
    "BETWEEN": "Between",
    "ISNULL": "Is Null",
    "ISNOTNULL": "Is not Null"
};

for(var name in expOperators ) {
    if(expOperators[name] === "Between"){
        document.write(name);
    }
}

http://jsfiddle.net/WsjyS/2/

答案 1 :(得分:0)

使用类似的东西

 for(var name in expOperators ) {
    if(expOperators.hasOwnProperty(name)){
      if(expOperators[name] === "Between"){
         alert(name);
       }
    }
}