Reputation: 4254
I have this:
var members: [
{
id:1,
user:{
name: 'John Smith',
email:'[email protected]'
}
},
{
id:2,
user:{
name: 'Jane Smith',
email:'[email protected]'
}
}]
I need the member object with email as the criteria.
I tried:
_.findWhere(members, {user.email: '[email protected]'})
No luck.
Upvotes: 1
Views: 1675
Reputation: 13505
Because you are not looking for a simple attribute AFAIK you can't use findWhere. Instead you can either use indexBy to get a rearranged collection (good for many similar lookups) or find with a test function (good for occasional lookups):
// http://jsfiddle.net/tshpfz0x/3/
var members = [{
id: 1,
user: {
name: 'John Smith',
email: '[email protected]'
}
}, {
id: 2,
user: {
name: 'Jane Smith',
email: '[email protected]'
}
}];
console.info(_.findWhere(members, {
user: {
email: '[email protected]'
}
})); // undefined
function byEmail(member) {return member.user.email;}
console.info(
_.indexBy(members, byEmail)["[email protected]"]
); // Object
console.info(
_.find(members, function (member) {
return (byEmail(member) === "[email protected]");})
); // Object
Upvotes: 2