如何判断关联数组是否有键?

发布于 2024-07-15 22:17:26 字数 127 浏览 8 评论 0原文

在 ActionScript 3 中,是否有任何方便的方法来确定关联数组(字典)是否具有特定键?

如果密钥丢失,我需要执行额外的逻辑。 我可以捕获未定义的属性异常,但我希望这是我最后的手段。

In ActionScript 3, is there any convenient way of determining if an associative array (dictionary) has a particular key?

I need to perform additional logic if the key is missing. I could catch the undefined property exception, but I'm hoping that can be my last resort.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

坦然微笑 2024-07-22 22:17:26
var card:Object = {name:"Tom"};

trace("age" in card);  //  return false 
trace("name" in card);  //  return true

尝试这个运算符:“in”

var card:Object = {name:"Tom"};

trace("age" in card);  //  return false 
trace("name" in card);  //  return true

Try this operator : "in"

顾冷 2024-07-22 22:17:26

hasOwnPropery 是测试它的一种方法。 以此为例:


var dict: Dictionary = new Dictionary();

// this will be false because "foo" doesn't exist
trace(dict.hasOwnProperty("foo"));

// add foo
dict["foo"] = "bar";

// now this will be true because "foo" does exist
trace(dict.hasOwnProperty("foo"));

hasOwnPropery is one way you test for it. Take this for example:


var dict: Dictionary = new Dictionary();

// this will be false because "foo" doesn't exist
trace(dict.hasOwnProperty("foo"));

// add foo
dict["foo"] = "bar";

// now this will be true because "foo" does exist
trace(dict.hasOwnProperty("foo"));
℉絮湮 2024-07-22 22:17:26

最快的方法可能是最简单的:

// creates 2 instances
var obj1:Object = new Object();
var obj2:Object = new Object();

// creates the dictionary
var dict:Dictionary = new Dictionary();

// adding the first object to the dictionary (but not the second one)
dict[obj1] = "added";

// checks whether the keys exist
var test1:Boolean = (dict[obj1] != undefined); 
var test2:Boolean = (dict[obj2] != undefined); 

// outputs the result
trace(test1,test2);

The quickest way may be the simplest:

// creates 2 instances
var obj1:Object = new Object();
var obj2:Object = new Object();

// creates the dictionary
var dict:Dictionary = new Dictionary();

// adding the first object to the dictionary (but not the second one)
dict[obj1] = "added";

// checks whether the keys exist
var test1:Boolean = (dict[obj1] != undefined); 
var test2:Boolean = (dict[obj2] != undefined); 

// outputs the result
trace(test1,test2);
笔芯 2024-07-22 22:17:26

hasOwnProperty 似乎是流行的解决方案,但值得指出的是它只处理字符串并且调用成本可能很高。

如果您使用对象作为字典中的键,则 hasOwnProperty 将不起作用。

更可靠、更高效的解决方案是使用严格相等来检查未定义。

function exists(key:*):Boolean {
    return dictionary[key] !== undefined;
}

请记住使用严格相等,否则具有空值但有效键的条目将看起来为空,即

null == undefined // true
null === undefined // false

实际上,正如前面提到的使用 in 也应该可以正常工作

function exists(key:*):Boolean {
    return key in dictionary;
}

hasOwnProperty seems to be the popular solution, but it's worth pointing out that it only deals in strings and can be expensive to call.

If you're using objects as keys in your Dictionary hasOwnProperty will not work.

The more reliable and performant solution is to use strict equality to check for undefined.

function exists(key:*):Boolean {
    return dictionary[key] !== undefined;
}

Remember to use strict equality otherwise entries with a null value but valid key will look empty i.e.

null == undefined // true
null === undefined // false

And actually, as has been mentioned using in should work fine too

function exists(key:*):Boolean {
    return key in dictionary;
}
握住我的手 2024-07-22 22:17:26

尝试这个:

for (var key in myArray) {
    if (key == myKey) trace(myKey+' found. has value: '+myArray['key']);
}

Try this:

for (var key in myArray) {
    if (key == myKey) trace(myKey+' found. has value: '+myArray['key']);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文