js根据数组中对象的某个属性值进行去重

120 阅读1分钟
var arr = [
  {
  from:'张三',
  to: '河南'
  },
  {
    from:'王二',
    to: '阿里'
  },
  {
    from:'王二',
    to: '杭州'
  },
  {
    from:'王二',
    to: '山东'
  },
]
//有如上数组,想根据数组中的对象的from属性进行去重,如果from一样的话,重复只保留一条。根据ES6属性编写函数代码如下:
 
function unique(arr1) {
  const res = new Map();
  return arr1.filter((a) => !res.has(a.from) && res.set(a.from, 1))
}

原文