来源:twitter
做用:Milos
译者:前端小智
为了保证的可读性,本文采用意译而非直译。前端
数组拷贝常常被误解,但这并非由于拷贝过程自己,而是由于缺少对 JS 如何处理数组及其元素的理解。JS 中的数组是可变的,这说明在建立数组以后还能够修改数组的内容。git
这意味着要拷贝一个数组,我们不能简单地将旧数组分配给一个新变量,它也是一个数组。若是这样作,它们将共享相同的引用,而且在更改一个变量以后,另外一个变量也将受到更改的影响。这就是咱们须要克隆这个数组的缘由。github
接着来看看一些关于拷贝何克隆数组的有趣方法和技巧。数组
Array.slice
方法const numbers = [1, 2, 3, 4, 5] const copy = numbers.slice() copy.push(6) // 添加新项以证实不会修改原始数组 console.log(copy) console.log(numbers) // 输出 // [1, 2, 3, 4, 5, 6] // [1, 2, 3, 4, 5]
Array.map
方法const numbers = [1, 2, 3, 4, 5] const copy = numbers.map( num => num ) copy.push(6) // 添加新项以证实不会修改原始数组 console.log(copy); console.log(numbers); // 输出 // [1, 2, 3, 4, 5, 6] // [1, 2, 3, 4, 5]
Array.from
方法const numbers = [1, 2, 3, 4, 5]; const copy = Array.from(new Set(numbers)); copy.push(6); // 添加新项以证实不会修改原始数组 console.log(copy); console.log(numbers); // 输出 // [1, 2, 3, 4, 5, 6] // [1, 2, 3, 4, 5]
const numbers = [1, 2, 3, 4, 5]; const copy = [...numbers]; copy.push(6); // 添加新项以证实不会修改原始数组 console.log(copy); console.log(numbers); // 输出 // [1, 2, 3, 4, 5, 6] // [1, 2, 3, 4, 5]
Array.of
方法和展开操做符const numbers = [1, 2, 3, 4, 5]; const copy = Array.of(...numbers); copy.push(6); // 添加新项以证实不会修改原始数组 console.log(copy); console.log(numbers); // 输出 // [1, 2, 3, 4, 5, 6] // [1, 2, 3, 4, 5]
Array.of()
方法建立一个具备可变数量参数的新数组实例,而不考虑参数的数量或类型。Array.of()
和 Array
构造函数之间的区别在于处理整数参数:Array.of(7
) 建立一个具备单个元素 7 的数组,而 Array(7)
建立一个长度为7
的空数组(注意:这是指一个有7个
空位(empty)的数组,而不是由7
个undefined
组成的数组)。微信
Array.of(7); // [7] Array.of(1, 2, 3); // [1, 2, 3] Array(7); // [ , , , , , , ] Array(1, 2, 3); // [1, 2, 3]
const numbers = [1, 2, 3, 4, 5]; const copy = new Array(...numbers); copy.push(6); // 添加新项以证实不会修改原始数组 console.log(copy); console.log(numbers); // 输出 // [1, 2, 3, 4, 5, 6] // [1, 2, 3, 4, 5]
const numbers = [1, 2, 3, 4, 5]; const [...copy] = numbers; copy.push(6); // 添加新项以证实不会修改原始数组 console.log(copy); console.log(numbers); // 输出 // [1, 2, 3, 4, 5, 6] // [1, 2, 3, 4, 5]
const numbers = [1, 2, 3, 4, 5]; const copy = numbers.concat(); copy.push(6); // 添加新项以证实不会修改原始数组 console.log(copy); console.log(numbers); // 输出 // [1, 2, 3, 4, 5, 6] // [1, 2, 3, 4, 5]
Array.push
方法和展开操做符const numbers = [1, 2, 3, 4, 5]; let copy = []; copy.push(...numbers); copy.push(6); // 添加新项以证实不会修改原始数组 console.log(copy); console.log(numbers); // 输出 // [1, 2, 3, 4, 5, 6] // [1, 2, 3, 4, 5]
Array.unshift
方法和展开操做符const numbers = [1, 2, 3, 4, 5]; let copy = []; copy.unshift(...numbers); copy.push(6); // 添加新项以证实不会修改原始数组 console.log(copy); console.log(numbers); // 输出 // [1, 2, 3, 4, 5, 6] // [1, 2, 3, 4, 5]
Array.forEach
方法和展开操做符const numbers = [1, 2, 3, 4, 5]; let copy = []; numbers.forEach((value) => copy.push(value)); copy.push(6); // 添加新项以证实不会修改原始数组 console.log(copy); console.log(numbers); // 输出 // [1, 2, 3, 4, 5, 6] // [1, 2, 3, 4, 5]
for
循环const numbers = [1, 2, 3, 4, 5]; let copy = []; for (let i = 0; i < numbers.length; i++) { copy.push(numbers[i]); } copy.push(6); // 添加新项以证实不会修改原始数组 console.log(copy); console.log(numbers); // 输出 // [1, 2, 3, 4, 5, 6] // [1, 2, 3, 4, 5]
Array.reduce
方法这个作法是可行,但比较多余,少用
const numbers = [1, 2, 3, 4, 5]; const copy = numbers.reduce((acc, x) => { acc.push(x); return acc; }, []); copy.push(6); // 添加新项以证实不会修改原始数组 console.log(copy); console.log(numbers); // 输出 // [1, 2, 3, 4, 5, 6] // [1, 2, 3, 4, 5]
apply
方法const numbers = [1, 2, 3, 4, 5];数据结构
let copy = []; Array.prototype.push.apply(copy, numbers); copy.push(6); // 添加新项以证实不会修改原始数组 console.log(copy); console.log(numbers); // 输出 // [1, 2, 3, 4, 5, 6] // [1, 2, 3, 4, 5]
代码部署后可能存在的BUG无法实时知道,过后为了解决这些BUG,花了大量的时间进行log 调试,这边顺便给你们推荐一个好用的BUG监控工具 Fundebug。app
原文:https://twitter.com/protic_milos函数
请注意,上面这些方法执行的是浅拷贝,就是数组是元素是对象的时候,我们更改对象的值,另外一个也会跟着变,就能技巧4来讲,若是我们的数组元素是对象,以下所示:工具
const authors = [ { name: '前端小智', age: 25 }, { name: '王大冶', age: 30 }, ] const copy = [...authors ] copy[0].name = '被更改过的前端小智' console.log(copy) console.log(authors)
输出学习
因此上面的技巧适合简单的数据结构,复杂的结构要使用深拷贝。数组拷贝常常被误解,但这并非由于拷贝过程自己,而是由于缺少对 JS 如何处理数组及其元素的理解。
干货系列文章汇总以下,以为不错点个Star,欢迎 加群 互相学习。
https://github.com/qq449245884/xiaozhi
由于篇幅的限制,今天的分享只到这里。若是你们想了解更多的内容的话,能够去扫一扫每篇文章最下面的二维码,而后关注我们的微信公众号,了解更多的资讯和有价值的内容。