javascript - Use underscore to return true or false with findWhere -


let's have following data:

var data = {     activeuser: { id: 3, name: 'joe', something: 'else' },     location: {         users: [{id: 1}, {id: 2}, {id: 3}]     } }; 

i want return boolean whether or not activeuser can found in data.location.users array. note objects in location.users array not have same keys activeuser object.

is there normal underscore way this? have following.

var userexists = (_.findwhere(data.location.users, {id: data.activeuser.id})) ? true : false; 

i'm using findwhere method either return object or null if doesn't exist.

one alternative option use ._some() method. return boolean based on whether found:

var userexists = _.some(data.location.users, function (user) {   return user.id === data.activeuser.id; }); 

Comments