问题描述
好了,很简单:不存在包含3个对象的数组。每个对象都有一个称为ID与1,2,或3。
Okay, very simple: there is an array containing 3 objects. Each object has a unique property called "ID" with values of either 1, 2, or 3.
一个对象被删除。
现在的目标是更新对应于新array.length值的每个对象的ID属性。
The objective now is to update the ID property of each object corresponding to the new array.length value.
因此,例如,为2 ID的对象删除了。阵列中其余对象将各自分别具有的1和3的ID值。
So for example, the object with ID of 2 got deleted. The remaining objects in the array would each have ID values of 1 and 3 respectively.
因此,目标是通过循环阵列和更新的ID属性为1,和2(代替1和3)。
So the objective is to loop through the array and update the ID properties to 1, and 2 (instead of 1 and 3).
所以我想这个问题是怎么写的一个循环中的数组更新每个元素的共同财产。谢谢。
So I guess the question is how to write a loop to update a common property of each element in an array. Thanks.
推荐答案
您可以使用一个for循环要经过阵列,在walkietokyo的答案,或者您也可以使用closure方法:
You can use a for-loop to go through the array, as in walkietokyo's answer, or you can use a method closure:
myArray.forEach ( function ( item:*, i:int, arr:Array) : void { item.ID = i; } );
或while循环:
or a while-loop:
var i:int = -1;
while (++i < myArray.length) myArray[i].ID = i;
这篇关于遍历阵列,每个元素的属性设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!