猿问

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

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'}]
30秒到达战场
浏览 277回答 2
2回答

喵喵时光机

varunique={};term_gpa.forEach(function(gpa){unique[JSON.stringify(gpa)]=gpa});term_gpa=Object.keys(unique).map(function(u){returnJSON.parse(u)});

暮色呼如

最快的方法是利用字典,遍历一次你的数组,将year和term设为数组的键:varmyArr=[];for(variteminterm_gpa){myArr[':'+item.year+':'+item.term]=item;};OK,myArr就是你要的去重的数组了。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答