The underline contains, based on the property of the object

I would like to use Underscore.js to determine if an instance of an object is present in an array.

Usage example:

var enrollments = [ { userid: 123, courseid: 456, enrollmentid: 1 }, { userid: 123, courseid: 456, enrollmentid: 2 }, { userid: 921, courseid: 621, enrollmentid: 3 } ] 

I want to be able to identify a unique registration in which the user IDs and courseid match.

So, basically, given the list of applications, I can remove duplicates based on matches with the user ID and courseid, but not with the registration ID.

+4
source share
1 answer

You can use the filter method from Underscore :

 function contains(arr, userid, courseid){ var matches = _.filter(arr, function(value){ if (value.userid == userid && value.courseid == courseid){ return value; } }); return matches; } contains(enrollments, 123, 456); 
+4
source

Source: https://habr.com/ru/post/1489302/


All Articles