JS 数组对象去重

原数据:

 let arr = [
        { "goodsId": "1", "quota": 12, "skuId": "1" },
        { "goodsId": "2", "quota": 12, "skuId": "2" },
        { "goodsId": "1", "quota": 12, "skuId": "1" }
    ]

去重后数据

 let arr = [
        { "goodsId": "1", "quota": 12, "skuId": "1" },
        { "goodsId": "2", "quota": 12, "skuId": "2" }
    ]

一、使用filter和Map  

function uniqueFunc(arr, uniId){
  const res = new Map();
  return arr.filter((item) => !res.has(item[uniId]) && res.set(item[uniId], 1));
}

二、使用reduce       

function uniqueFunc2(arr, uniId){
  let hash = {}
  return arr.reduce((accum,item) => {
    hash[item[uniId]] ? '' : hash[item[uniId]] = true && accum.push(item)
    return accum
  },[])
}

三、for循环            

function uniqueFunc3(arr, uniId){
  let obj = {}
  let tempArr = []
  for(var i = 0; i

你可能感兴趣的:(JavaScript,javascript,前端,java)