The following code causes both elements from id 0
to be set to -
, even though I want only one to be set to -1
. Am I just creating a re
Yes. Both valueArray
and labelArray
reference the same underlying array. To make a copy, use slice():
valueArray = labelArray.slice(0);
NOTE: Slice() only copies 1 level deep, which works fine for primitive arrays. If the array contains complex objects, use something like jQuery's clone(), credit @Jonathan.
valueArray
is just a reference to labelArray
.
What you want to do is clone the array. You can do this using jQuery.clone() or a similar cloning function.
Am I just creating a reference to the labelArray […] ?
Yes, exactly. valueArray
and labelArray
still identify the same object, which hasn't been copied.