const numbers = [3, 6, 9]; const numbersCopy = Array.from(numbers); numbers === numbersCopy; // => false
只拷贝了一层,这是单层浅拷贝
下面是深拷贝(多层数组拷贝)
function recursiveClone(val) { return Array.isArray(val) ? Array.from(val, recursiveClone) : val; } const numbers = [[0, 1, 2], ['one', 'two', 'three']]; const numbersClone = recursiveClone(numbers); numbersClone; // => [[0, 1, 2], ['one', 'two', 'three']] numbers[0] === numbersClone[0] // => false
https://blog.csdn.net/JaxsonWang/article/details/101496076