Skip to content

Instantly share code, notes, and snippets.

@chaitanyagupta
Created August 22, 2010 08:13
Show Gist options
  • Save chaitanyagupta/543519 to your computer and use it in GitHub Desktop.
Save chaitanyagupta/543519 to your computer and use it in GitHub Desktop.
var findItem = function(jid, items){
for(var i = 0; i < items.length; ++i){
var item = items[i];
if(item.jid === jid){
return item;
}
}
return null;
}
var findItem = function(jid, items){
var foundItem = null;
dojo.some(items, function(item){
return (item.jid === jid) ? (foundItem = item, true) : false;
});
return foundItem;
}
@chaitanyagupta
Copy link
Author

Haha, you've just implemented Common Lisp's some; I was wondering if there was a more concise solution apart from writing some in JS. If Javascript's Array.some behaved like this rather than returning just a boolean, it would have been so much more useful.

@pathsny
Copy link

pathsny commented Aug 24, 2010

oh, yeah some is pretty neat in that it returns the value. I like that. Ruby's array.find does this, that's why I called it find. There is also an "any" which is meant to be used like some. Except you can rely on it even if your array contains false or nil ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment