ES6數組去重的經常使用方法

數組去重的經常使用方法彙總: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

相關文章
相關標籤/搜索