0

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?

2
  • Can you try using this statement? nodeArray[index].splice(0,1,childrenIDs.slice(0)); Commented Jul 20, 2015 at 17:53
  • 1
    I just tried it. It worked! Thank you very much. Please post as an answer for anyone else with this problem. Commented Jul 20, 2015 at 17:57

2 Answers 2

2

Can you try using this statement?

nodeArray[index].splice(0,1,childrenIDs.slice(0));

Sign up to request clarification or add additional context in comments.

Comments

1

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.