javascript - 如何从对象数组中获取具有特定键的唯一值的对象?
问题描述
我有一个对象数组,如下所示。
[{
"_id": {
"$oid": "5d9a16764f66ef0017a738ee"
},
"user": "tVH3U5Va4cBFiATvAACD",
"team": "team1",
"question": "5d98c7109d0e1d0017e3d31f",
"date": {
"$date": "2019-10-06T16:29:40.240Z"
},
"correct": "true",
"ownId": "4",
"blockId": "1"
},
{
"_id": {
"$oid": "5d9a167c4f66ef0017a738ef"
},
"user": "tVH3U5Va4cBFiATvAACD",
"team": "team1",
"question": "5d98c7109d0e1d0017e3d31f",
"date": {
"$date": "2019-10-06T16:29:46.694Z"
},
"correct": "true",
"ownId": "4",
"blockId": "1"
},
{
"_id": {
"$oid": "5d9a16824f66ef0017a738f0"
},
"user": "tVH3U5Va4cBFiATvAACD",
"team": "team1",
"question": "5d98c7109d0e1d0017e3d31e",
"date": {
"$date": "2019-10-06T16:29:52.900Z"
},
"correct": "true",
"ownId": "5",
"blockId": "1"
}]
我需要使用 last 获取date
具有唯一user
性ownId
和blockId
. 唯一我的意思是我只会得到一个user
相同ownId
的blockId
。对于此示例,我只想获取,因为数组中的第一个对象和数组中的最后一个对象具有相同user
的 ,ownId
和blockId
.
[{
"_id": {
"$oid": "5d9a167c4f66ef0017a738ef"
},
"user": "tVH3U5Va4cBFiATvAACD",
"team": "team1",
"question": "5d98c7109d0e1d0017e3d31f",
"date": {
"$date": "2019-10-06T16:29:46.694Z"
},
"correct": "true",
"ownId": "4",
"blockId": "1"
},
{
"_id": {
"$oid": "5d9a16824f66ef0017a738f0"
},
"user": "tVH3U5Va4cBFiATvAACD",
"team": "team1",
"question": "5d98c7109d0e1d0017e3d31e",
"date": {
"$date": "2019-10-06T16:29:52.900Z"
},
"correct": "true",
"ownId": "5",
"blockId": "1"
}]
我尝试的是遍历数组,但是这样我只能在一个键中获得唯一的对象。我不知道如何用几个键来拥有它。
stat.forEach(function(item) {
var i = unique.findIndex(x => x.user == item.user);
if (i <= -1) {
unique.push({
id: item._id,
user: item.user
});
}
});
解决方案
您可以使用reduce()方法迭代数组,以构建哈希表或对象,并将其user + ownId + blockId
作为属性名称或哈希键。
迭代时,如果存在具有相同键的对象,则比较日期并将值替换为具有最新日期的对象。
var data = [{ "_id": { "$oid": "5d9a16764f66ef0017a738ee" }, "user": "tVH3U5Va4cBFiATvAACD", "team": "team1", "question": "5d98c7109d0e1d0017e3d31f", "date": { "$date": "2019-10-06T16:29:40.240Z" }, "correct": "true", "ownId": "4", "blockId": "1" }, { "_id": { "$oid": "5d9a167c4f66ef0017a738ef" }, "user": "tVH3U5Va4cBFiATvAACD", "team": "team1", "question": "5d98c7109d0e1d0017e3d31f", "date": { "$date": "2019-10-06T16:29:46.694Z" }, "correct": "true", "ownId": "4", "blockId": "1" }, { "_id": { "$oid": "5d9a16824f66ef0017a738f0" }, "user": "tVH3U5Va4cBFiATvAACD", "team": "team1", "question": "5d98c7109d0e1d0017e3d31e", "date": { "$date": "2019-10-06T16:29:52.900Z" }, "correct": "true", "ownId": "5", "blockId": "1" } ];
var result = Object.values(data.reduce((acc, curr) => {
let value = acc[curr.user + curr.ownId + curr.blockId];
if (!value || new Date(curr.date.$date) > new Date(value.date.$date)) {
acc[curr.user + curr.ownId + curr.blockId] = curr;
}
return acc;
}, {}));
console.log(result);
推荐阅读
- rest - 如何通过 Rest-API 在 Airwatch/Workspace ONE 中设置设备名称(友好名称)
- flutter - TextField 输入翻倍
- node.js - 在 axios get request Network Error 中生成错误.. 可能的未处理的 Promise Rejection (id: 0):
- reference - 如何调整 Latex 参考文献中作者姓名的顺序(Journal of Finance 风格)
- ruby-on-rails - 带条件的 Rails 验证
- mysql - BTREE 或 HASH 作为枚举索引的类型是否更小?
- javascript - 使用 mermaid-js 以角度处理异步数据
- java - 如何删除 Maven 错误聚合器项目需要“pom”作为包装
- cluster-computing - 对一个 SLURM 作业使用不同的 Intel CPU 类型时出现本地长度错误
- python - 检查一个数组是否是另一个数组python的元素