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.