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?
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
itemand then subtract that from thechildIndex()of the matched result using E4X search.