Skip to content

Instantly share code, notes, and snippets.

@davidwood
Created September 13, 2011 14:13
Show Gist options
  • Save davidwood/1213888 to your computer and use it in GitHub Desktop.
Save davidwood/1213888 to your computer and use it in GitHub Desktop.
Find an object in an array by id property
var myId = 10;
var index;
var item = _.detect(this._list, function(val, indx) {
if (val.id == myId) {
index = indx;
return true;
}
}, this);
if (index) {
console.log('Matching index is: ' + index);
console.log('Matching item is : ' + item);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment