[js数组]请问怎么对数组中的对象去重?

后台输出的接口数据需要前端来去掉重复。数据如:

var arr = [{
    "name": "ZYTX",
    "age": "Y13xG_4wQnOWK1QwJLgg11d0pS4hewePU95UHtpMl3eE81uS74NC-6zu-Rtnw4Ix",
    "gender": "AAAAAA.doc"
}, {
    "name": "ZYTA",
    "age": "Y13xG_4wQnOWK1QwJLgg11d0pS4hewePU95UHtpMl3eE81uS74NC-6zu-Rtnw4Ix",
    "gender": "BBBBBB.doc"
}, {
    "name": "ZDTX",
    "age": "Y13xG_4wQnOWK1QwJLgg11d0pS4hewePU95UHtpMl3eE81uS74NC-6zu-Rtnw4Ix",
    "gender": "CCCCCC.doc"
}, {
    "name": "ZYTX",
    "age": "Y13xG_4wQnOWK1QwJLgg11d0pS4hewePU95UHtpMl3eE81uS74NC-6zu-Rtnw4Ix",
    "gender": "AAAAAA.doc"
}];

最后应该输出

var arr = [{
    "name": "ZYTX",
    "age": "Y13xG_4wQnOWK1QwJLgg11d0pS4hewePU95UHtpMl3eE81uS74NC-6zu-Rtnw4Ix",
    "gender": "AAAAAA.doc"
}, {
    "name": "ZYTA",
    "age": "Y13xG_4wQnOWK1QwJLgg11d0pS4hewePU95UHtpMl3eE81uS74NC-6zu-Rtnw4Ix",
    "gender": "BBBBBB.doc"
}, {
    "name": "ZDTX",
    "age": "Y13xG_4wQnOWK1QwJLgg11d0pS4hewePU95UHtpMl3eE81uS74NC-6zu-Rtnw4Ix",
    "gender": "CCCCCC.doc"
}];

实际上是按照 name 唯一的来的,数组只需要留下一个唯一的name,不管这个nameage或者gender是什么,反正随便留下来一个唯一name

阅读 20.1k
7 个回答
var arr = [{
    "name": "ZYTX",
    "age": "Y13xG_4wQnOWK1QwJLgg11d0pS4hewePU95UHtpMl3eE81uS74NC-6zu-Rtnw4Ix",
    "gender": "AAAAAA.doc"
}, {
    "name": "ZYTA",
    "age": "Y13xG_4wQnOWK1QwJLgg11d0pS4hewePU95UHtpMl3eE81uS74NC-6zu-Rtnw4Ix",
    "gender": "BBBBBB.doc"
}, {
    "name": "ZDTX",
    "age": "Y13xG_4wQnOWK1QwJLgg11d0pS4hewePU95UHtpMl3eE81uS74NC-6zu-Rtnw4Ix",
    "gender": "CCCCCC.doc"
}, {
    "name": "ZYTX",
    "age": "Y13xG_4wQnOWK1QwJLgg11d0pS4hewePU95UHtpMl3eE81uS74NC-6zu-Rtnw4Ix",
    "gender": "AAAAAA.doc"
}];
var hash = {};
arr = arr.reduce(function(item, next) {
    hash[next.name] ? '' : hash[next.name] = true && item.push(next);
    return item
}, [])
console.log(arr);
var set = arr.reduce(
  function(set, item) {
    set[item["name"]] = item;
    return set;
  },
  {}
);

Object.keys(set).map(function(key) { return set[key]; });

如果支持ES6标准的话,直接使用Set对象进行去重就好了

var rs = new Set(arr);

毕竟集合是不包含重复元素的

如果考虑效率的话,
function unique(arr) {

var result = [], hash = {};
for (var i = 0, f_code; (f_code = arr[0]) != null; i++) {
    if (!hash[f_code]) {
        result.push(f_code);
        hash[f_code] = true;
    }
}
return result;

}
http://blog.csdn.net/zhihua_w...推荐楼主去看看这篇文章

废话不多说,直接上代码:啦啦啦

var nArr=[
  {
    id:10001,
    name:'张三'
  },{
    id:10002,
    name:'李四'
  },{
    id:10001,
    name:'张三'
  },{
    id:10003,
    name:'王五'
  },{
    id:10004,
    name:'赵六'
  },{
    id:10003,
    name:'王五'
  },{
    id:10006,
    name:'zuoqi'
  }
];

// 方案一,利用新的数组进行循环遍历
// var newArr=[];
// function _objIsInArray(obj,arr){
//   let tmpStatus=false;
//   for(let j=0;j<arr.length;j++){
//     if(obj.id==arr[j].id){
//       return j;
//       break;
//     }else{
//       tmpStatus=false;
//     }
//   }
//   if(!tmpStatus){
//     return -1;
//   }
// }
// for(let i=0;i<nArr.length;i++){
//   if(_objIsInArray(nArr[i],newArr) ==-1){
//      newArr.push(nArr[i]);
//   }else{
//     console.log('对象已存在');
//   }
// }
// console.log(newArr);
新手上路,请多包涵

unique(arr) {

  const res = new Map();
  return arr.filter((a) => !res.has(a.name) && res.set(a.name, 1));
},
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏