javascript - 过滤具有多个值的对象数组
问题描述
我有两个对象数组。虽然localDataArray
is 已经存储在我的应用程序中,但remoteUpdateDataArray
来自后端。
var localDataArray = [
{ "date": "10/01/19", "category": "surf", "hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"},
{ "date": "10/01/19", "category": "skate", "hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"},
{ "date": "10/01/19", "category": "skate", "hash": "54fd1711209fb1c0781092374132c66e79e2241b"}
];
var remoteUpdateDataArray = [
{ "date": "12/01/19", "category": "surf", "hash": "4a0a19218e082a343a1b17e5333409af9d98f0f5"},
{ "date": "11/01/19", "category": "surf", "hash": "54fd1711209fb1c0781092374132c66e79e2241b"},
{ "date": "10/01/19", "category": "surf", "hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"},
{ "date": "10/01/19", "category": "skate", "hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"},
{ "date": "10/01/19", "category": "skate", "hash": "54fd1711209fb1c0781092374132c66e79e2241b"}
];
我想从中删除所有重复的对象remoteUpdateDataArray
。每个对象的唯一标识符是哈希。
到目前为止,我有以下代码:
let hashValue = "54fd1711209fb1c0781092374132c66e79e2241b"
var filteredResult = remoteUpdateDataArray.filter(x => x.hash !== hashValue);
结果:
var filteredResult = [
{ "date": "12/01/19", "category": "surf", "hash": "4a0a19218e082a343a1b17e5333409af9d98f0f5"},
{ "date": "11/01/19", "category": "surf", "hash": "54fd1711209fb1c0781092374132c66e79e2241b"},
{ "date": "10/01/19", "category": "surf", "hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"},
{ "date": "10/01/19", "category": "skate", "hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"}
];
我如何设法摆脱数组中的另一个(在这种情况下是两个重复的对象)?请记住,这些数组可能会变得非常大。
解决方案
我会从你的第一个数组中构建一个哈希列表(以保存迭代),然后简单地使用 include() 过滤
const inLocalData = localDataArray.map(({hash: e}) => e);
const result = remoteUpdateDataArray.filter(({hash: e}) => ! inLocalData.includes(e));
console.log(result);
<script>
var localDataArray = [{
"date": "10/01/19",
"category": "surf",
"hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"
},
{
"date": "10/01/19",
"category": "skate",
"hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"
},
{
"date": "10/01/19",
"category": "skate",
"hash": "54fd1711209fb1c0781092374132c66e79e2241b"
}
];
var remoteUpdateDataArray = [{
"date": "12/01/19",
"category": "surf",
"hash": "4a0a19218e082a343a1b17e5333409af9d98f0f5"
},
{
"date": "11/01/19",
"category": "surf",
"hash": "54fd1711209fb1c0781092374132c66e79e2241b"
},
{
"date": "10/01/19",
"category": "surf",
"hash": "da39a3ee5e6b4b0d3255bfef95601890afd80709"
},
{
"date": "10/01/19",
"category": "skate",
"hash": "a0f1490a20d0211c997b44bc357e1972deab8ae3"
},
{
"date": "10/01/19",
"category": "skate",
"hash": "54fd1711209fb1c0781092374132c66e79e2241b"
}
];
</script>
推荐阅读
- c# - C# 9.0 记录可以支持字典和列表等数据结构吗?
- c - scanf 返回 0,即使输入已给出
- php - 如何在 PHPMailer 中设置优先级
- python-3.x - 在 Python 3 中编写一个与 .txt 文件一起使用的函数
- python-3.x - 如何在下面创建递归函数?
- r - 如何在 r 中使用 multiroot 和基于矩阵的函数
- python - 在 python 中使用 pynput 和 playsound 模块时鼠标冻结/非常滞后
- angularjs - 角种子项目中的预启动和预测试脚本
- mathml - 我如何知道 mathml 文档使用的数学版本?
- algorithm - 三个不同列的组合的最小总和,其中数字在不同的行中