js根据多个属性去重

版本1:

function process(arr, ...rest) {
  const cache = [];
  let t;
  function check(item) {
    let trueList = [];
    let args;
    for(args of rest) {
      trueList.push(item[args] === t[args])
    }
    return trueList.every(item => item)
  }
  for (t of arr) {
    if (cache.find(check)) {
    continue;
  }
    cache.push(t);
  }
  return cache;
}

版本2:

function unique(list, ...rest) {
  if (Object.prototype.toString.call(list).indexOf('Array') < 0 || list.length === 0) return []
  const result = {}
  for (const item of list) {
    const key = rest.map(key => item[key]).join('|')
    result[key] = item
  }
  return Object.values(result)
}

 

posted @ 2020-01-17 14:28  W-it-H-ou-T  阅读(609)  评论(1)    收藏  举报