數組去重的經常使用方法彙總:javascript
[...newSet(test)];
Array.from()
const test = ['q', 'w', 'e', 'q', 'u', 'p'] Array.from(new Set(test))
function unique(arr) { const res = new Map(); return arr.filter((a) => !res.has(a) && res.set(a, 1)) }
數組中對象去重;html
let test = [ { imageId: '1', imageUrl: 'https://raw.githubusercontent.com/perfectSymphony/vue-admin/master/src/assets/logo.png', }, { imageId: '1', imageUrl: 'https://raw.githubusercontent.com/perfectSymphony/vue-admin/master/src/assets/logo.png', }, { imageId: '2', imageUrl: 'https://raw.githubusercontent.com/perfectSymphony/vue-admin/master/src/assets/logo.png', }, { imageId: '3', imageUrl: 'https://raw.githubusercontent.com/perfectSymphony/vue-admin/master/src/assets/logo.png', }, { imageId: '3', imageUrl: 'https://raw.githubusercontent.com/perfectSymphony/vue-admin/master/src/assets/logo.png', }, { imageId: '4', imageUrl: 'https://raw.githubusercontent.com/perfectSymphony/vue-admin/master/src/assets/logo.png', } ]
objTrim: function(){ let obj = {}; this.test= this.test.reduce((cur,next) => { obj[next.imageId] ? "" : obj[next.imageId] = true && cur.push(next); return cur; },[]); return this.test; },
歡迎issue!!!vue
原文出處:https://www.cnblogs.com/myprogramer/p/12107575.htmljava