Created
August 22, 2010 08:13
-
-
Save chaitanyagupta/543519 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var findItem = function(jid, items){ | |
var foundItem = null; | |
dojo.some(items, function(item){ | |
return (item.jid === jid) ? (foundItem = item, true) : false; | |
}); | |
return foundItem; | |
} |
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
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.