http://prototypejs.org/api/enumerable/any

Man that feels good pointing to the "official docs"

but seriously this *should* work.

var someArray = ['jim', 'marge', 'timmeh', 'bob'];

if( someArray.any(function(n){return n == 'bob';}) ) {
  alert('found bob')
}

syntax is a bit weird if you aren't used to it, but once you are it is
very powerful.

Nicholas

On 1/19/07, Kjell Bublitz <[EMAIL PROTECTED]> wrote:

Hi

i was wondering if there is a quick way to search for a item within an
array. Here is my approach for this functionality. It's an prototype
for an Array method called "has":

Array.prototype.has = function(needle) {
        for (var i=0;i<this.length;i++) {
                if(this[i] == needle){ return true; }
        }
        return false;
}

var someArray = ['jim', 'marge', 'timmeh', 'bob'];

if( someArray.has('bob') )
{
    alert('found bob')
}

Isn't there something like "has()" in prototype already? I have seen
the find / findAll, but the example in the docs are kinda complex for
"just that".. could someone break it down for me please? i would much
appreciate.

TIA, Kjell

>



--
DCRails.com || Making the Metrorail fun!
http://www.captureimportant.info || Sidewalk || Bring People to You

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on 
Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to