为了账号安全,请及时绑定邮箱和手机立即绑定

js数组中 删除重复对象 有什么快速的方法

js数组中 删除重复对象 有什么快速的方法

MMTTMM 2019-04-21 20:20:56
varterm_gpa=[{year:'2013-2014',term:'1'},{year:'2013-2014',term:'1'},{year:'2013-2014',term:'1'},{year:'2013-2014',term:'1'},{year:'2013-2014',term:'1'},{year:'2013-2014',term:'1'},{year:'2013-2014',term:'1'},{year:'2013-2014',term:'1'},{year:'2013-2014',term:'1'},{year:'2013-2014',term:'2'},{year:'2013-2014',term:'2'},{year:'2013-2014',term:'2'},{year:'2013-2014',term:'2'},{year:'2013-2014',term:'2'},{year:'2013-2014',term:'2'},{year:'2013-2014',term:'2'},{year:'2013-2014',term:'2'},{year:'2013-2014',term:'2'},{year:'2013-2014',term:'2'},{year:'2013-2014',term:'2'},{year:'2013-2014',term:'2'},{year:'2014-2015',term:'1'},{year:'2014-2015',term:'1'},{year:'2014-2015',term:'1'},{year:'2014-2015',term:'1'},{year:'2014-2015',term:'1'},{year:'2014-2015',term:'1'},{year:'2014-2015',term:'1'},{year:'2014-2015',term:'1'},{year:'2014-2015',term:'1'},{year:'2014-2015',term:'1'},{year:'2014-2015',term:'1'},{year:'2014-2015',term:'1'},{year:'2014-2015',term:'1'},{year:'2014-2015',term:'1'}]比如上面那个要变成varterm_gpa=[{year:'2013-2014',term:'1'},{year:'2013-2014',term:'2'},{year:'2014-2015',term:'1'}]
查看完整描述

2 回答

?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

varunique={};
term_gpa.forEach(function(gpa){unique[JSON.stringify(gpa)]=gpa});
term_gpa=Object.keys(unique).map(function(u){returnJSON.parse(u)});
                            
查看完整回答
反对 回复 2019-04-21
?
小怪兽爱吃肉

TA贡献1852条经验 获得超1个赞

最快的方法是利用字典,遍历一次你的数组,将year和term设为数组的键:varmyArr=[];
for(variteminterm_gpa){
myArr[':'+item.year+':'+item.term]=item;
};
OK,myArr就是你要的去重的数组了。
                            
查看完整回答
反对 回复 2019-04-21
  • 2 回答
  • 0 关注
  • 269 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信