I have an array of objects like below.
[{
"_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"
}]
I need to get objects with last date
, which has unique user
, ownId
and blockId
. By unique I mean that I will get only one user
with same ownId
same blockId
. For this example I want to get only, since first object in array and last object in array has same user
, ownId
and 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"
}]
What I tried is to itterate through array, but this way I can get only unique object within one key. I can't figure out how to have it with few keys.
stat.forEach(function(item) {
var i = unique.findIndex(x => x.user == item.user);
if (i <= -1) {
unique.push({
id: item._id,
user: item.user
});
}
});