数组基本操作

42 阅读1分钟

数组基本操作

数组对象根据字段去重

export const uniqueArrayObject = (arr = [], key = 'id') => {  
  if (arr.length === 0return  
  let list = []  
  const map = {}  
  arr.forEach((item) => {  
    if (!map[item[key]]) {  
      map[item[key]] = item  
    }  
  })  
  list = Object.values(map)  
  
  return list  
}
const responseList = [  
    { id1name'树哥' },  
    { id2name'黄老爷' },  
    { id3name'张麻子' },  
    { id1name'黄老爷' },  
    { id2name'张麻子' },  
    { id3name'树哥' },  
    { id1name'树哥' },  
    { id2name'黄老爷' },  
    { id3name'张麻子' },  
]  
  
uniqueArrayObject(responseList, 'id')  
// [{ id: 1, name: '树哥' },{ id: 2, name: '黄老爷' },{ id: 3, name: '张麻子' }]

根据属性值找下标

this.markerData.findIndex(item => item.shopImportDesignateId == row.shopImportDesignateId)