Find All Objects With Matching Ids Javascript
I'm trying to get all objects with matching id's from my students array and get other property values from them... For instance my array looks like this: const students = [ {id
Solution 1:
I would look into the filter function. It's build into JavaScript.
Here's an example of how it works. All you need to do is find a way to make a function that will tell if it has the proper id.
functionisBigEnough(value) {
return value >= 10;
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered is [12, 130, 44]
Solution 2:
If you see the documentation for _.find
it states
Iterates over elements of collection, returning the first element predicate returns truthy for.
You should use the _.filter
method for what you want
Iterates over elements of collection, returning an array of all elements predicate returns truthy for.
Something like
const result = _.filter(students, {student_id: studentId});
Solution 3:
const result = students.filter(e => e.id === 1);
Post a Comment for "Find All Objects With Matching Ids Javascript"