1

如果我知道来自定义为的对象的值,我正在尝试获取属性名称

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"
};

显然,如果我需要找到价值,那将很容易,但我需要完成相反的任务,知道价值找出属性是什么。任何想法?

4

2 回答 2

0

使用这样的东西

 for(var name in expOperators ) {
    if(expOperators.hasOwnProperty(name)){
      if(expOperators[name] === "Between"){
         alert(name);
       }
    }
}
于 2012-11-15T06:13:18.630 回答
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/

于 2012-11-14T21:57:11.010 回答