When copying an array in JavaScript to another array:(将JavaScript中的数组复制到另一个数组时:)
var arr1 = ['a','b','c'];
var arr2 = arr1;
arr2.push('d'); //Now, arr1 = ['a','b','c','d']
I realized that arr2
refers to the same array as arr1
, rather than a new, independent array.(我意识到arr2
指向与arr1
相同的数组,而不是一个新的独立数组。) How can I copy the array to get two independent arrays?(如何复制数组以获取两个独立的数组?)
ask by Dan translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…