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

var term_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' } ]

比如上面那个要变成
var term_gpa = [{ year: '2013-2014', term: '1' },
{ year: '2013-2014', term: '2' },
{ year: '2014-2015', term: '1' } ]

阅读 16.2k
5 个回答
var unique = {};
term_gpa.forEach(function(gpa){ unique[ JSON.stringify(gpa) ] = gpa });
term_gpa = Object.keys(unique).map(function(u){return JSON.parse(u) }); 

最快的方法是利用字典,遍历一次你的数组,将year和term设为数组的键:
var myArr = []; for(var item in term_gpa){ myArr[':'+item.year +':'+item.term] = item; };
OK, myArr就是你要的去重的数组了。

对象其实不能相互比较的,因为占据的内存不同。不过可以采用其他的方法来做,你看看我写的这个吧数组去重

新手上路,请多包涵
var a = [{ x: 10, y: 20, z: 30 }, { x: 10, y: 20, z: 30 }, { x: 50, y: 60, z: 60 }, { x: 10, y: 20, z: 30 }, { x: 20, y: 20, z: 30 }];
        console.log(a);
        function unique(arr) {
            var res = [arr[0]];
            for (var i = 0; i < arr.length; i++) {
                var repeat = false;
                for (let j = 0; j < res.length; j++) {
                    if (isObjectValueEqual(arr[i], res[j])) {
                        repeat = true;
                        break;
                    }
                }
                if (!repeat) {
                    res.push(arr[i]);
                }
            }
            return res;
        }
        b = unique(a);
        console.log(b);
        //判断两对象是否相等
        function isObjectValueEqual(a, b) {
            var aProps = Object.getOwnPropertyNames(a);
            var bProps = Object.getOwnPropertyNames(b);
            if (aProps.length != bProps.length) {
                return false;
            }
            for (var i = 0; i < aProps.length; i++) {
                var propName = aProps[i];
                if (a[propName] !== b[propName]) {
                    return false;
                }
            }
            return true;
        }

javascript去除数组中重复的对象

array.filter() 新增的数组方法

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏