### 1、JavaScript 展开操作符（浅拷贝）

``````numbers = [1, 2, 3];
numbersCopy = [...numbers];
``````

``````numbersCopy.push(4);
console.log(numbers, numbersCopy);
// [1, 2, 3] and [1, 2, 3, 4]
// numbers is left alone
``````

``````nestedNumbers = [[1], [2]];
numbersCopy = [...nestedNumbers];

numbersCopy[0].push(300);
console.log(nestedNumbers, numbersCopy);
// [[1, 300], [2]]
// [[1, 300], [2]]
// They've both been changed because they share references
``````

### 2、for() 循环（浅拷贝）

``````numbers = [1, 2, 3];
numbersCopy = [];

for (i = 0; i < numbers.length; i++) {
numbersCopy[i] = numbers[i];
}
``````

``````numbersCopy.push(4);
console.log(numbers, numbersCopy);
// [1, 2, 3] and [1, 2, 3, 4]
// numbers is left alone
``````

``````nestedNumbers = [[1], [2]];
numbersCopy = [];

for (i = 0; i < nestedNumbers.length; i++) {
numbersCopy[i] = nestedNumbers[i];
}

numbersCopy[0].push(300);
console.log(nestedNumbers, numbersCopy);
// [[1, 300], [2]]
// [[1, 300], [2]]
// They've both been changed because they share references
``````

### 3、while() 循环（浅拷贝）

`for` 循环类似——不太简洁，不够描述型......但是它总归也能用！

``````numbers = [1, 2, 3];
numbersCopy = [];
i = -1;

while (++i < numbers.length) {
numbersCopy[i] = numbers[i];
}
``````

``````numbersCopy.push(4);
console.log(numbers, numbersCopy);
// [1, 2, 3] and [1, 2, 3, 4]
// numbers is left alone
``````

``````nestedNumbers = [[1], [2]];
numbersCopy = [];

i = -1;

while (++i < nestedNumbers.length) {
numbersCopy[i] = nestedNumbers[i];
}

numbersCopy[0].push(300);
console.log(nestedNumbers, numbersCopy);
// [[1, 300], [2]]
// [[1, 300], [2]]
// They've both been changed because they share references
``````

### 4、Array.map（浅拷贝）

``````numbers = [1, 2, 3];
double = (x) => x * 2;

numbers.map(double);
``````

#### 拷贝呢？

``````numbers = [1, 2, 3];
numbersCopy = numbers.map((x) => x);
``````

`map(identity)` 拷贝一个列表。

``````identity = (x) => x;
numbers.map(identity);
// [1, 2, 3]
``````

### 5、Array.filter（浅拷贝）

``````[1, 2, 3].filter((x) => x % 2 === 0);
// [2]
``````

``````numbers = [1, 2, 3];
numbersCopy = numbers.filter(() => true);
``````

### 6、Array.reduce（浅拷贝）

``````numbers = [1, 2, 3];

numbersCopy = numbers.reduce((newArray, element) => {
newArray.push(element);

return newArray;
}, []);
``````

`reduce` 循环遍历列表时，转换初始值。

### 7、Array.slice（浅拷贝）

`slice` 根据你提供的开始/结束索引返回数组的浅拷贝副本。

``````[1, 2, 3, 4, 5].slice(0, 3);
// [1, 2, 3]
// Starts at index 0, stops at index 3
``````

``````numbers = [1, 2, 3, 4, 5];
numbersCopy = numbers.slice();
// [1, 2, 3, 4, 5]
``````

### 8、JSON.parse 和 JSON.stringify（深拷贝）

`JSON.stringify` 将一个对象转换成一个字符串。

`JSON.parse` 将一个字符串转换成一个对象。

``````nestedNumbers = [[1], [2]];
numbersCopy = JSON.parse(JSON.stringify(nestedNumbers));

numbersCopy[0].push(300);
console.log(nestedNumbers, numbersCopy);

// [[1], [2]]
// [[1, 300], [2]]
// These two arrays are completely separate!
``````

### 9、Array.concat（浅拷贝）

`concat` 将数组与值或其他数组组合。

``````[1, 2, 3].concat(4); // [1, 2, 3, 4]
[1, 2, 3].concat([4, 5]); // [1, 2, 3, 4, 5]
``````

``````[1, 2, 3].concat(); // [1, 2, 3]
[1, 2, 3].concat([]); // [1, 2, 3]
``````

### 10、Array.from（浅拷贝）

``````numbers = [1, 2, 3];
numbersCopy = Array.from(numbers);
// [1, 2, 3]
``````