2

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.

One of the objects gets deleted.

The objective now is to update the ID property of each object corresponding to the new array.length value.

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.

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.

3 Answers 3

2

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; } );

or a while-loop:

var i:int = -1;
while (++i < myArray.length) myArray[i].ID = i;
Sign up to request clarification or add additional context in comments.

Comments

0
for (var i:uint = 1; i <= myArray.length; i++) {
    myArray[i].ID = i;
}

General info on loops: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7fcf.html

Comments

0
var i:uint; // for speed keep out of the loop
var arrayLength = myArray.length // for speed keep out of the loop

for (i = 0; i < arrayLength; i++) {
    myArray[i].ID = i;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.