es5 方式
在 es5 中,我们可以使用 for 循环配合数组方法来实现对象数组的去重。
function uniquearraybyproperty(array, property) {
var seen = {};
return array.filter(function(item) {
var val = item[property];
return seen.hasownproperty(val) ? false : (seen[val] = true);
});
}
var arr = [
{ id: 1, name: 'alice' },
{ id: 2, name: 'bob' },
{ id: 1, name: 'alice' }, // 重复
{ id: 3, name: 'charlie' }
];
var uniquearr = uniquearraybyproperty(arr, 'id');
console.log(uniquearr);
// 输出:[ { id: 1, name: 'alice' }, { id: 2, name: 'bob' }, { id: 3, name: 'charlie' } ]
es6 及之后的方式
随着 es6 的引入,有了更多简洁的方式来处理数组和对象。
使用 set 和 json.stringify / json.parse
这种方法适用于简单对象,但需要注意 json.stringify 会丢失原型链上的属性,而且对于包含循环引用的对象会抛出错误。
function uniquearraybypropertyes6(array, property) {
const seen = new set();
return array.filter(item => {
const val = json.stringify(item[property]);
return seen.has(val) ? false : seen.add(val);
});
}
var uniquearres6 = uniquearraybypropertyes6(arr, 'id');
console.log(uniquearres6);
使用 map
对于更复杂的对象去重,可以使用 map 来存储已经见过的对象。
function uniquearraybypropertywithmap(array, property) {
const map = new map();
return array.filter(item => {
const val = item[property];
if (!map.has(val)) {
map.set(val, true);
return true;
}
return false;
});
}
var uniquearrwithmap = uniquearraybypropertywithmap(arr, 'id');
console.log(uniquearrwithmap);
使用 map 结合自定义比较逻辑
这种方法适用于对象具有多个属性的情况,可以自定义比较逻辑。
function uniquearraybyproperties(array, properties) {
const seen = new map();
return array.filter(item => {
const key = properties.map(prop => item[prop]).join('|');
if (!seen.has(key)) {
seen.set(key, true);
return true;
}
return false;
});
}
const arr = [
{ id: 1, name: 'alice' },
{ id: 2, name: 'bob' },
{ id: 1, name: 'alice' }, // 重复
{ id: 3, name: 'charlie' }
];
const uniquearr = uniquearraybyproperties(arr, ['id', 'name']);
console.log(uniquearr);
// 输出:[ { id: 1, name: 'alice' }, { id: 2, name: 'bob' }, { id: 3, name: 'charlie' } ]
使用 lodash 库
lodash 提供了一个方便的函数 _.uniqby,可以轻松实现去重。
const _ = require('lodash');
const uniquearr = _.uniqby(arr, 'id');
console.log(uniquearr);
// 输出:[ { id: 1, name: 'alice' }, { id: 2, name: 'bob' }, { id: 3, name: 'charlie' } ]
使用 weakmap
weakmap 可以用来存储对象引用,而不会阻止这些对象被垃圾回收。虽然 weakmap 的键必须是对象,但它可以用来存储已经处理过的对象。
function uniquearraybypropertywithweakmap(array, property) {
const seen = new weakmap();
return array.filter(item => {
const val = item[property];
if (!seen.has(val)) {
seen.set(val, true);
return true;
}
return false;
});
}
const uniquearrwithweakmap = uniquearraybypropertywithweakmap(arr, 'id');
console.log(uniquearrwithweakmap);
// 输出:[ { id: 1, name: 'alice' }, { id: 2, name: 'bob' }, { id: 3, name: 'charlie' } ]
使用 set 和 weakref (实验性)
在最新的 javascript 版本中,weakref 可以用来存储对象的弱引用,这可以用来辅助对象的去重,但请注意 weakref 是一个实验性功能,可能在未来的版本中有所变化。
const { weakref } = require('weakref'); // 假设这是一个支持 weakref 的环境
function uniquearraybypropertywithweakref(array, property) {
const seen = new set();
return array.filter(item => {
const ref = new weakref(item[property]);
if (!seen.has(ref)) {
seen.add(ref);
return true;
}
return false;
});
}
const uniquearrwithweakref = uniquearraybypropertywithweakref(arr, 'id');
console.log(uniquearrwithweakref);
// 输出:[ { id: 1, name: 'alice' }, { id: 2, name: 'bob' }, { id: 3, name: 'charlie' } ]
使用 set 结合 json.stringify
对于简单对象,可以使用 json.stringify 来生成对象的字符串表示,然后使用 set 进行去重。
function uniquearraybypropertywithset(array, property) {
const seen = new set();
return array.filter(item => {
const val = json.stringify(item[property]);
return seen.has(val) ? false : seen.add(val);
});
}
const uniquearrwithset = uniquearraybypropertywithset(arr, 'id');
console.log(uniquearrwithset);
// 输出:[ { id: 1, name: 'alice' }, { id: 2, name: 'bob' }, { id: 3, name: 'charlie' } ]
总结
以上列出了一些实现 javascript 对象数组去重的不同方法(项目中经常出现这样的需求数组对象去重)。选择哪种方法取决于你的具体需求和环境。对于简单的对象,使用 set 和 json.stringify 可能是最简单的方式;而对于更复杂的情况,使用 map 或者 weakmap 可能更合适。如果使用第三方库,lodash 的 _.uniqby 函数提供了便捷的解决方案。对于实验性的功能,如 weakref,则需要考虑兼容性和稳定性问题。
以上就是js对象去重的多种方式小结的详细内容,更多关于js对象去重的资料请关注代码网其它相关文章!
发表评论