简体   繁体   English

Javascript将数组分配给另一个数组的元素

[英]Javascript assigning array to element of another array

I have a list of unique ID's of a parent nodes children stored into childrenIDs. 我有一个存储在childrenIDs中的父节点child的唯一ID列表。

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 然后,我将这些ID分配给另一个数组的第二个元素

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. 当在控制台中输入nodeArray [index] [0]时,将输出正确的输出(包含所有ID)。 However, if I type childrenIDs.length = 0 to clear the first array, calling nodeArray[index][0] produces a null output. 但是,如果我输入childrenIDs.length = 0来清除第一个数组,则调用nodeArray [index] [0]会产生空输出。 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]. 好像nodeArr [index] [0]几乎充当着指向childID的指针,就像清除childID一样,nodeArray [index] [0]也是如此。

I need to be able to reuse childrenIDs. 我需要能够重用childrenID。 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? 我清除数组的方式是否有问题,清除子代ID后是否有办法将数据保留在nodeArray [index] [0]中?

您可以尝试使用此语句吗?

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM