vue两个数组合并删除重复项
时间: 2023-09-03 09:15:23 浏览: 110
可以使用 ES6 中的 Set 数据结构来帮助删除重复项。具体实现如下:
```javascript
const array1 = [1, 2, 3];
const array2 = [2, 3, 4];
const mergedArray = [...new Set([...array1, ...array2])];
console.log(mergedArray); // [1, 2, 3, 4]
```
首先,将两个数组合并成一个新数组,使用数组的扩展运算符 (...) 将数组中的元素展开。然后使用 Set 数据结构来去除重复项,Set 会自动去除重复项,最后再将新的 Set 转换回数组形式,使用扩展运算符 (...) 将 Set 中的元素展开即可得到最终的合并数组。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)