如何判断关联数组是否有键?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试这个运算符:“in”
Try this operator : "in"
hasOwnPropery
是测试它的一种方法。 以此为例:hasOwnPropery
is one way you test for it. Take this for example:最快的方法可能是最简单的:
The quickest way may be the simplest:
hasOwnProperty 似乎是流行的解决方案,但值得指出的是它只处理字符串并且调用成本可能很高。
如果您使用对象作为字典中的键,则 hasOwnProperty 将不起作用。
更可靠、更高效的解决方案是使用严格相等来检查未定义。
请记住使用严格相等,否则具有空值但有效键的条目将看起来为空,即
实际上,正如前面提到的使用
in
也应该可以正常工作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.
Remember to use strict equality otherwise entries with a null value but valid key will look empty i.e.
And actually, as has been mentioned using
in
should work fine too尝试这个:
Try this: