js去除数组中重复对象

近期遇到一个项目,后台反的数据有重复,需要去重,去重的办法很多,这里用到了reduce去除数组中的重复数据,亲测可用
首先给大家看下我的数据格式

var list = [
    {"id":"1","score":"基础分"},
    {"id":"2","score":"社会贡献"},
    {"id":"2","score":"社会贡献"},
    {"id":"3","score":"工程质量"},
    {"id":"4","score":"信用评价"},
]

能看到id为2是重复的 需要去掉重复值,重新组成数组 直接上代码

    var hash = {};
    var newData = [...list]
    list = newData.reduce(function (item, next) {
     hash[next.value] ? '' : hash[next.value] = true && item.push(next);
        return item
      }, [])

输出list 得出结果

var list = [
    {"id":"1","score":"基础分"},
    {"id":"2","score":"社会贡献"},
    {"id":"3","score":"工程质量"},
    {"id":"4","score":"信用评价"},
]

Es6新语法set去重数组

const numbers = [1, 1, 20, 3, 3, 3, 9, 9];
const uniqueNumbers = [...new Set(numbers)];
console.log(uniqueNumbers)

//js数组对象删除掉某一项选中的对象返回新的数组

    const handleChange = (value) => {
    //选中的对象id
      console.log(value);
      //原数组深拷贝给新数组
      let list = JSON.parse(JSON.stringify(stackUserState.list));
      for (var i = 0; i < list.length; i++) {
        if (list[i].userUuid == value) {
        //删除选中的那条数据
          list.splice(i, 1);
        }
      }
      //赋值给原数组
      stackUserState.list = list;
    };

版权声明:本文为CSDN_bare原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。