1

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.

1
  • {user.email … is a syntax error! Commented Sep 24, 2014 at 14:00

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.