javascript - JS - 使用 lodash 删除数组中的重复对象
问题描述
例如我有这个数组:
0: {myfield: "1f974a20-aa59-11e8-9653-ab3419ed3bc9", order: 0, value: ""}
1: {myfield: "1f974a20-aa59-11e8-9666-ab3419ed3bc9", order: 0, value: ""}
2: {myfield: "1f974a20-aa59-11e8-9653-ab3419ed3bc9", order: 0, value: "One"}
3: {myfield: "af7401a0-aa6e-11e8-9653-ab3419ed3bc9", order: 1, value: "Two"}
我希望能够遍历它并弹出/删除旧版本的重复数组,在此示例中,我想保留对象 2并弹出/删除对象 0,因为它们都具有完全相同的 myfield
是否可以使用 lodash 做到这一点?
解决方案
您可以使用_.uniqBy()
let arr = [ {myfield: "1f974a20-aa59-11e8-9653-ab3419ed3bc9", order: 0, value: ""}, {myfield: "1f974a20-aa59-11e8-9666-ab3419ed3bc9", order: 0, value: ""}, {myfield: "1f974a20-aa59-11e8-9653-ab3419ed3bc9", order: 0, value: "One"}, {myfield: "af7401a0-aa6e-11e8-9653-ab3419ed3bc9", order: 1, value: "Two"}];
let result = _.uniqBy(arr, 'myfield');
推荐阅读
- python - 如何通过仅更改最后一行代码来使以下功能起作用?
- html - Django Crispy 表单在浏览器中返回缺失的标签
- dictionary - 在 Julia 中从 pandas.Series 复制 map()
- java - Java 控制器如何与 JSP 通信?
- javascript - 有没有办法在三元表达式的条件中返回值而不在 JavaScript 中重新指定它?
- package - 在 package.json 中调用脚本会导致“脚本名称:未找到”错误
- node.js - 如何让我的 nodejs 函数等待 forEach 的 fetch 完成?
- python - 使用opencv从相机到tkinter窗口的实时视频馈送
- reactjs - React Native:找不到商店。确保遵循这些说明
- python-3.x - AWS Lambda 上的 Firefox Selenium:二进制不是 Firefox 可执行文件