JS数组及数组对象去重原创

原创
小哥 3年前 (2022-10-26) 阅读数 79 #大杂烩

js阵列中重复的对象将减轻负担。

https://www.cnblogs.com/shiyunfront/p/7423977.html

var arr = [
  {
    "value": 1,
    "label": "小明"
  },
  {
    "value": 2,
    "label": "小红"
  },
  {
    "value": 1,
    "label": "小明"
  },
  {
    "value": 3,
    "label": "小花"
  }
];
var hash = {};
arr = arr.reduce(function(item, next) {
  hash[next.value] ?  : hash[next.value] = true && item.push(next);
  return item
}, [])
console.log(arr);

打印结果:


注意:

hash[next. value ] ? : hash[next. value ] = true && item.push(next);

在上面的代码中 value为了让密钥名称变得沉重key

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

热门