How to Retain Original Array?

How to Retain Original Array?

rajankur6494
Advocate Advocate
9 Views
2 Replies
Message 1 of 3

How to Retain Original Array?

rajankur6494
Advocate
Advocate

[ FlexSim 21.0.10 ]

Hi Team,

I am having array of length 8. Array = [1,2,3,4,5,6,7,8]

After picking 2 from array, I am able to get the remaining part using splice function. [1,3,4,5,6,7,8]

But, it is changing the original array also to the same value.

I have tried to make copy of array which is having reference of original array. But, even in this case it is changing the original value of array.

How can I retain the original array?

Thank you!

0 Likes
Accepted solutions (1)
10 Views
2 Replies
Replies (2)
Message 2 of 3

moehlmann_fe
Observer
Observer
Accepted solution

If you set a variable to an existing array, the variable is only really a 'reference' to the original array. Any changes are thus directly applied to the original.

To get an independent copy of the array, use '.clone()'.

// This changes the original array
Array nums = originalArray;
nums.splice(...);

// This does not
Array nums = originalArray.clone();
nums.splice(...);

The same is true for tables.

0 Likes
Message 3 of 3

rajankur6494
Advocate
Advocate
Thank you @Felix Möhlmann !
0 Likes