查明 AS3 的 XMLList 是否包含字符串作为节点值
是否有与 Array.indexOf 等效的 XMLList?
例如——
var array:Array = ['one','two'];
trace(array.indexOf('two')); // returns 1, since it's at the second position
trace(array.indexOf('three')); // returns -1, since it isn't found
……对吧?但是如果我得到了这个该怎么办 -
var xml:XML = <list><item>one</item><item>two</item><list>
var xmlList:XMLList = list.item;
必须有一种更简单的方法来检查 XMLList 中的某个节点是否具有特定值,而不是循环遍历所有节点,对吗?类似于 -
xmlList.indexOfChildByNodeValue('two'); // returns 1, because it's the second child
xmlList.indexOfChildByNodeValue('three'); // returns -1, because it doesn't match any children
有道理吗?
Is there an XMLList equivalent to Array.indexOf?
For example -
var array:Array = ['one','two'];
trace(array.indexOf('two')); // returns 1, since it's at the second position
trace(array.indexOf('three')); // returns -1, since it isn't found
... right? but what if I've got this -
var xml:XML = <list><item>one</item><item>two</item><list>
var xmlList:XMLList = list.item;
there's got to be an easier way to check to see if one of the nodes in the XMLList has a particular value than looping through all of them, right? something akin to -
xmlList.indexOfChildByNodeValue('two'); // returns 1, because it's the second child
xmlList.indexOfChildByNodeValue('three'); // returns -1, because it doesn't match any children
make sense?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我整理了一个演示,展示如何在不循环的情况下完成此操作。尽管它确实需要更多的前期工作。不过,如果您要经常检查值,那么这是有回报的。
基本思想是,存储第一个
item
的索引,然后使用 E4X 搜索从匹配结果的childIndex()
中减去该索引。I put together a demo of how you can do it without looping. Though it does call for a little more upfront work. Though it pays off if you are going to be checking values a lot.
The basic idea is that you store the index of the first
item
and then subtract that from thechildIndex()
of the matched result using E4X search.我认为唯一的选择是循环遍历 XMLList。您的另一个选择是使用: 包含() 不过,如果您实际上只想知道是否
indexOf() == || ,这只会对您的情况有所帮助。 != -1
。我不确定 XMLList 实际上是如何存储在底层 C++ 中的,但如果它确实只是一个数组,那么线性搜索 O(n) 是最好的选择。
I think the only bet is to loop through the XMLList. The other option you have is to use: contains() Though this will only help in your situation if you actually only want to know whether
indexOf() == || != -1
.I am not sure of how the XMLList is actually stored in the underlying C++, but if it truly is just an array, then a linear search O(n) is the best you are going to do.