question

Ankur A3 avatar image
0 Likes"
Ankur A3 asked Ankur A3 commented

How to Retain Original Array?

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!

FlexSim 21.0.10
arraychangevalueoriginal
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

·
Felix Möhlmann avatar image
0 Likes"
Felix Möhlmann answered Ankur A3 commented

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.

· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Ankur A3 avatar image Ankur A3 commented ·
Thank you @Felix Möhlmann !
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.