简体   繁体   English

为什么此功能会变异数据?

[英]Why does this function mutate data?

function bubbleSort(toSort) {
  let sort = toSort;
  let swapped = true;
  while(swapped) {
    swapped = false;
    for(let i = 0; i < sort.length; i++) {
      if(sort[i-1] > sort[i]) {
        let temp = sort[i-1];
        sort[i-1] = sort[i];
        sort[i] = temp;
        swapped = true;
      }
    }
  }
  return sort;
}

let asdf = [1,4,3,2];
let asd = bubbleSort(asdf);

console.log(asdf, asd);

The output to this code is: [ 1, 2, 3, 4 ] [ 1, 2, 3, 4 ]. 该代码的输出为:[1,2,3,4] [1,2,3,4]。

What I would expect: [ 1, 4, 3, 2 ] [ 1, 2, 3, 4 ]. 我期望的是:[1,4,3,2] [1,2,3,4]。

What I'm wondering, is why does this mutate the asdf variable? 我想知道的是为什么这会使asdf变量突变? The bubbleSort function takes the given array (asdf), makes a copy of it (sort), and then deals with that variable and returns it, which asd is set equal to. bubbleSort函数采用给定的数组(asdf),对其进行复制(排序),然后处理该变量并将其返回,并将asd设置为等于。 I feel like an idiot but I have no clue why this is :( 我觉得自己是个白痴,但我不知道为什么这是:(

The bubbleSort function takes the given array (asdf), makes a copy of it (sort) bubbleSort函数采用给定的数组(asdf),并对其进行复制(排序)

No, it doesn't. 不,不是。 Assignment doesn't make a copy of an object, it creates another reference to an existing object. 分配不会复制对象,它会创建对现有对象的另一个引用。

A simple way to copy an array is to use Array.prototype.slice : 复制数组的一种简单方法是使用Array.prototype.slice

  let sort = toSort.slice( 0 );

For more on copying objects in general see: How do I correctly clone a JavaScript object? 有关一般复制对象的更多信息,请参见: 如何正确克隆JavaScript对象?

You are sorting the input list given as a function argument which is mutable. 您正在对作为可变参数给出的输入列表进行排序。 When you assign the list to a new variable, it doesn't create a 'copy' it just creates another reference pointing to the same list, same data, which you then go ahead a sort. 当您将列表分配给新变量时,它不会创建“副本”,而只是创建另一个指向相同列表,相同数据的引用,然后进行排序。 This is why both asdf and add variables are the same, because they are two variables that point to the same memory location, same data. 这就是为什么asdf和add变量都相同的原因,因为它们是两个指向相同内存位置,相同数据的变量。

If you wish to copy the array so not to modify the input array, have a look into the javascript slice() method. 如果您希望复制数组而不修改输入数组,请查看javascript slice()方法。

You need to clone the original array to avoid the change happening in original array. 您需要克隆原始阵列,以避免原始阵列发生更改。 clone can be done by using 克隆可以通过使用

  1. slice() prototype method. slice()原型方法。
  2. loop . 循环
  3. Array.from() . Array.from()
  4. concat() . concat()

Replace let sort = toSort; 替换let sort = toSort; with let sort = toSort.slice(0); let sort = toSort.slice(0); or 要么

 let sort=[];
  for(var i=0;i < toSort.length;i++){
  sort[i]=toSort[i];  
  }

or 要么

  let sort = Array.from(toSort);

or 要么

let sort = toSort.concat();

 function bubbleSort(toSort) { let sort = toSort.slice(0); let swapped = true; while (swapped) { swapped = false; for (let i = 0; i < sort.length; i++) { if (sort[i - 1] > sort[i]) { let temp = sort[i - 1]; sort[i - 1] = sort[i]; sort[i] = temp; swapped = true; } } } return sort; } let asdf = [1, 4, 3, 2]; let asd = bubbleSort(asdf); console.log(asdf, asd); 

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

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