简体   繁体   English

jQuery AS3中的each()函数

[英]jquery each() function in AS3

Though flash is dying technology I still have to finish my project :))) 尽管闪存正在逐渐消亡,但我仍然必须完成我的项目:)))

How to implement for each loop in AS3 that gives me the index of the element in the array, much like the jquery each() function does it 如何在AS3中为每个循环实现,该循环为我提供了数组中元素的索引,就像jquery each()函数那样

jQuery.each( collection, callback(indexInArray, valueOfElement) )

I use this 我用这个

for each(var myObj:* in myArray)
{ 
     // do some stuff with myObj
}

How to use stateme like this myArray.splice(index, 1) in for each??? 如何为每个使用像myArray.splice(index,1)这样的stateme ???

Or is it possible at all? 还是有可能吗?

jQuery's .each() is just an object iterator that does some additional checking for you. jQuery的.each()只是一个对象迭代器,它为您做一些附加检查。 You should be able to get away with a syntax like: 您应该可以使用如下语法:

var i = 0;
for ( property in object ) {
    callback( i++, object[property] )
}

Though, I don't know how AS3 works, but keep in mind that there is no order to object properties in JavaScript. 虽然,我不知道AS3如何工作,但是请记住,JavaScript中没有对象属性的顺序。

This would work. 这会起作用。

var arr:Array = new Array();
arr[1] = true;
arr[5] = true;
arr.forEach(function (item:*, index:int, array:Array)
{
    if(item != undefined)
    {
        callback(index, item);          
    }        
});

Why not using the forEach function belonging to the Array object that will executes a function on each item in the array. 为什么不使用属于Array对象的forEach函数,该函数将对数组中的每个项目执行一个函数。

Here a live sample using it : http://wonderfl.net/c/mLhCM 这是一个使用它的实时示例: http : //wonderfl.net/c/mLhCM

ex: 例如:

var ar:Array = ["hello", 1, "world", 2];
ar.forEach(
   function(elm:*, index:int, arr:Array):void {
            trace("got elm : "+elm+" at index : "+index);
   }
);

Why not simple for...in loop: 为什么for...in循环不那么简单:

var arr:Array = [2,1,5,0,0,0,87,8,110];

for (var i:String in arr)
{
    trace("Value:" + arr[i] + " at " + i);
}

Do you mean something like this? 你的意思是这样吗?

for(var i:int=0; i<myArray.length; i++){
    var myObj = myArray[i];
}

it's hard to know exactly what to use unless you specify what you wish to achieve :) 除非您指定要实现的目标,否则很难确切地知道要使用什么:

a "for each" should not be used to iterate through an indexed array, the standard for(var i=0;xxx;xxx) should be used. 不应使用“ for each”来遍历索引数组,而应使用for(var i = 0; xxx; xxx)的标准。
If for whatever reason you still find yourselff needing a for each loop then this would be how to get the index 如果由于某种原因您仍然发现自己每个循环都需要a,那么这将是获取索引的方法

var arr:Array = ['00','11','22','33','44','55','66','77','88','99']
for each(var o:Object in arr){
    trace(arr.indexOf(o))
}

Another option would be to use the for in loop 另一种选择是使用for循环

for (var key:String in arr){
    trace(key)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM