我有两个数组,
const pets = ["dog", "cat", "hamster"]
const wishlist = ["bird", "snake"]
我想将wishlist
添加到pets
中,有两种方法可以实现:
方法1:
pets.push.apply(pets,wishlist)
转换后的结果为:[ 'dog', 'cat', 'hamster', 'bird', 'snake' ]
方法2:
pets.push(...wishlist)
这也导致了:[ 'dog', 'cat', 'hamster', 'bird', 'snake' ]
当我处理更大的数据时,这两种方法在性能方面有区别吗?
pets.push.apply
的代码。无论如何,影响应用速度的性能差异几乎微乎其微。你为什么会有这个疑问呢? - user663031.concat()
更为合适。 - a better oliver