Javascript。。。使用新数组值的rest运算符行为?

Javascript。。。使用新数组值的rest运算符行为?,javascript,rest,ecmascript-6,Javascript,Rest,Ecmascript 6,我有下面的代码,传入的值是4,1,9,14,6,8和 分配给newHeight的值是1、4、6、8、9、14。插入排序按升序对数组进行排序 var heightChecker = function(heights) { var sorted = [...heights]; var newHeight = insertionSort(heights); var count = 0; for(var i = 0; i < newHeight.leng

我有下面的代码,传入的值是4,1,9,14,6,8和 分配给newHeight的值是1、4、6、8、9、14。插入排序按升序对数组进行排序

var heightChecker = function(heights) {

    var sorted = [...heights];
    var newHeight = insertionSort(heights);
    var count = 0;
    
    for(var i = 0; i < newHeight.length; i++) {
        if(newHeight[i] !== sorted[i]) {
            count++;
        }
    }

    return count;
}
它将答案返回为0

我不明白为什么答案不一样,在调试和谷歌搜索之后,我仍然找不到原因

这是插入排序代码

function insertionSort(inputArr) {
    let n = inputArr.length;
    for (let i = 1; i < n; i++) {
        // Choosing the first element in our unsorted subarray
        let current = inputArr[i];
        // The last element of our sorted subarray
        let j = i-1;
        while ((j > -1) && (current < inputArr[j])) {
            inputArr[j+1] = inputArr[j];
            j--;
        }
        inputArr[j+1] = current;
        console.log(inputArr);
    }
    return inputArr;
}
函数插入排序(inputArr){
设n=inputArr.length;
for(设i=1;i-1)和&(当前
a=[…b]
创建
b
的副本,
a=b
仅为您的值指定不同的名称(即指向相同值的第二个引用)

或者,通过函数调用:

function addNumber(array, number) {
  array.push(number);
}
let a = [1,2,3];
b = a;
c = [...a];
addNumber(b, 4); // now a = [1,2,3,4]; and b = [1,2,3,4] (=a); c = [1,2,3]
addNumber(c, 5); // still a = [1,2,3,4]; but c = [1,2,3,5]

您是否也可以从insertionSort函数发回代码刚刚添加了insertionSort函数代码我刚刚编辑了我的代码,通过我的新编辑,您的评论是否仍然正确?我不是在第一个函数中指定参数,而是在循环中使用参数。将数组分配给不同的变量不会复制数组的内容,但会使用rest运算符(或
.slice
)复制数组的内容。
let a = [1,2,3];
b = a;
c = [...a];
a[1] = 41; // modifies value of a AND b
b[1] = 42; // modifies value of a AND b
c[1] = 43; // only modifies value of c
console.log(a); // 1, 42, 3
console.log(b); // 1, 42, 3
console.log(c); // 1, 43, 3
function addNumber(array, number) {
  array.push(number);
}
let a = [1,2,3];
b = a;
c = [...a];
addNumber(b, 4); // now a = [1,2,3,4]; and b = [1,2,3,4] (=a); c = [1,2,3]
addNumber(c, 5); // still a = [1,2,3,4]; but c = [1,2,3,5]