I can't seem to find an agreed-upon way to find an object in an array of objects by a single field, specifically a large string field such as a Mongo id. For example I have the following array:
[
{
_id: "55e4a11251e4c7914426xxxx,
name: 'John'
}, {
_id: "55e4a11251e4c7914426yyyy",
name: 'Jack
}
]
I now want to create a function to return the object from the array where the _id is equal. I have the following, but it seems like it could be improved upon:
function getObject(searchedId, array) {
for (var i = 0; i < array.length; i++) {
if (array[i]._id === searchedId) {
return array[i];
}
}
}