I have a mongoDB document with an array of objects field looking like this:
"leaving_users" : [
{
"user_id" : "FZ78Pr82JPz66Gc3p",
"leaving_date" : ISODate("2015-06-12T14:14:14.501Z")
}
]
Can I use _.pluck to obtain the leaving_date related to a specific user_id?
My code seems to work fine but I wanted to check this is the right way to do it, and be sure that I won't end up sometimes with a different index if I use a _.pluck function.
Here is my code:
if (doc.leaving_users //guarding
//if the user belongs to the leaving_users object array
&& _.pluck(doc.leaving_users, "user_id").indexOf(userId) != -1
//if his leaving_date field is after yesterday
&& doc.leaving_users[_.pluck(doc.leaving_users, "user_id").indexOf(userId)].leaving_date > yesterday)
{
leftRecently = true;
} else{
leftRecently = false;
}
Bonus question: how would you make that more elegant?
Yes, the indexes will be the same. This is clear if you look at the implementation of
_.pluck:...and the implementation of
_.map:That said, I wouldn't call
_.plucktwice like that, and I don't think I'd use_.pluckfor this at all, I'd use_.findIndexand save the result: