Loop through array of objects to find object with matching property

arrays, javascript, loops

Solution

You'll have to loop through `users`:

var i = users.length,
    ownerData;

while(i--) {
    if(selectedGroup.owner == users[i].id) {
        ownerData = users[i];
        break;
    }
}

Or you could use `Array.filter()`:

var ownerData = users.filter(function(user) {
    return user.id === selectedGroup.owner;
})[0];

Problem

I have 1 array, one with a list of all my users with unique IDs. I have an object which contains contains a selected groups information. Part of that information is the owners ID. I'm trying to figure out, how do I get the users's information given the groups owner ID? For example, the student group object has an owner ID of 70, there's a user on my sites who's ID is 70...how do I match them up? ``` users: [ { id: 68 name: mike domain: i:0#.f|admembers|mike.ca email: mike.ca isAdmin: False }, etc etc ] selectedGroup: { name: Students id: 78 description: owner: 70 ownerIsUser: True } ```

Original source