gpt4 book ai didi

javascript - 如何从对象数组中获取具有特定键的唯一值的对象?

转载 作者:行者123 更新时间:2023-11-30 10:59:54 27 4
gpt4 key购买 nike

我有一个对象数组,如下所示。

 [{
"_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"
}]

我需要获取具有最后date 的对象,它具有唯一的userownIdblockId。唯一性是指我只会获得一个具有相同 ownId 相同 blockIduser。对于这个例子,我只想获取,因为数组中的第一个对象和数组中的最后一个对象具有相同的 userownIdblockId

[{
"_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);

关于javascript - 如何从对象数组中获取具有特定键的唯一值的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58259570/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com