I have a list of unique ID's of a parent nodes children stored into childrenIDs.
var childrenIDs=["8b69b08e-d75e-6ef6-2cf4-275ff130cd74","42325602-9312-3565-b7dc-37383ca53c17", "2c91dcd6-7436-eff5-393e-cea8cbef338c"]
I then assign those IDs to the second element of another array
nodeArray[index].splice(0,1,childrenIDs);
When nodeArray[index][0] is entered into the console, the right output (containing all of the IDs) is printed. However, if I type childrenIDs.length = 0 to clear the first array, calling nodeArray[index][0] produces a null output. It seems as if nodeArr[index][0] is almost acting as a pointer to childrenIDs in the way that when childrenIDs is cleared, so is nodeArray[index][0].
I need to be able to reuse childrenIDs. Is there something wrong with how I am clearing the array and is there a way for me to preserve the data in nodeArray[index][0] after clearing childrenIDs?
-
Can you try using this statement? nodeArray[index].splice(0,1,childrenIDs.slice(0));ashokd– ashokd2015年07月20日 17:53:09 +00:00Commented Jul 20, 2015 at 17:53
-
1I just tried it. It worked! Thank you very much. Please post as an answer for anyone else with this problem.kevbud007– kevbud0072015年07月20日 17:57:50 +00:00Commented Jul 20, 2015 at 17:57
2 Answers 2
Can you try using this statement?
nodeArray[index].splice(0,1,childrenIDs.slice(0));
Comments
Instead of assigning the array as child, you should assign a copy of it.
childrenIDs.slice(0)
Which would look like this:
nodeArray[index].splice(0,1,childrenIDs.slice(0));