0

I'm trying to assign array2 to array1 by using: array2 = array1, but the problem is, and as it's mentioned here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html

Array assignment is by reference rather than by value. When you assign one array variable to another array variable, both refer to the same array

I thought of using a for-loop, and it worked perfectly, but I feel like there must be faster and more simple.

Here's my loop, if anyone's interested:

for (var n:int = 0; n < array1.length; n++)
{
    array2[n] = array1[n];
}

So, I was wondering, Is there a way to assign array2 to array1 without using a loop?

1 Answer 1

3

You can use the slice method:

Returns a new array that consists of a range of elements from the original array, without modifying the original array. The returned array includes the startIndex element and all elements up to, but not including, the endIndex element.

//default values of the parameters will return a copy of the array array2 = array1.slice();

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.