当前位置: 代码网 > it编程>编程语言>Javascript > JS对象去重的多种方式小结

JS对象去重的多种方式小结

2024年10月28日 Javascript 我要评论
es5 方式在 es5 中,我们可以使用for循环配合数组方法来实现对象数组的去重。function uniquearraybyproperty(array, property) { var see

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对象去重的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com