vue中数组对象去重
时间: 2023-11-01 09:58:20 浏览: 134
在Vue中对数组对象进行去重有多种方法。一种简单的方法是使用Set集合来去重,但是对于数组对象来说,这种方法并不适用。可以采取以下间接的方法来实现数组对象的去重。
一种方法是使用Map来进行去重。首先创建一个空的Map对象,然后使用filter方法对数组进行筛选,只保留Map中没有出现过的元素,并将新的元素添加到Map中。最后返回筛选后的数组即可。例如:
```
elimination(arr) {
const res = new Map();
return arr.filter((item) => !res.has(item.id) && res.set(item.id, 1));
},
```
在Vue中使用该方法可以这样调用:
```
this.arr = this.elimination(this.arr);
```
另一种方法是直接在Vue的方法中使用filter和Map来进行去重。例如:
```
quChong() {
let arr = [
{ id: 1, name: '111' },
{ id: 1, name: '111' },
{ id: 2, name: '222' },
{ id: 3, name: '333' }
];
let arr1 = this.unique(arr);
console.log(arr1);
},
unique(arr) {
const res = new Map();
return arr.filter((item) => !res.has(item.id) && res.set(item.id, 1));
},
```
以上代码中,通过调用`unique`方法实现了数组对象的去重,并将结果打印输出。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [数组对象去重(vue)](https://blog.csdn.net/m0_58259728/article/details/120197737)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [vue数组去重,数组对象去重](https://blog.csdn.net/snowflak/article/details/127653450)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)