On Friday 19 January 2007 16:46, Kjell Bublitz 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 >include() does this Cheers, Dave -- ---------------------- Author Ajax in Action http://manning.com/crane Ajax in Practice http://manning.com/crane2 Prototype & Scriptaculous Quickly http://manning.com/crane3 --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Thomas Fuchs
2007-Jan-19 16:57 UTC
Re: Prototype.js: Is there an "in_array"-like function?
[''a'',''b'',''c''].include(''b'') --> true See http://prototypejs.org/api/enumerable/include Best, Thomas Am 19.01.2007 um 17:46 schrieb Kjell Bublitz:> > 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 > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Nicholas Schlueter
2007-Jan-19 16:59 UTC
Re: Prototype.js: Is there an "in_array"-like function?
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 <m3nt0r.de-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Nicholas Schlueter
2007-Jan-19 17:01 UTC
Re: Prototype.js: Is there an "in_array"-like function?
ok yours is better, you win :) On 1/19/07, Thomas Fuchs <t.fuchs-moWQItti3gBl57MIdRCFDg@public.gmane.org> wrote:> > [''a'',''b'',''c''].include(''b'') --> true > > See http://prototypejs.org/api/enumerable/include > > Best, > Thomas > > > Am 19.01.2007 um 17:46 schrieb Kjell Bublitz: > > > > > 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Kjell Bublitz
2007-Jan-19 17:05 UTC
Re: Prototype.js: Is there an "in_array"-like function?
Nah.. both won.. include() is not strict.. with your example i could force that by using ==so... i asked for one function and got myself two.. Thank you :) PS: include should get a boolean modifier to enable strict comparsion. On 1/19/07, Nicholas Schlueter <schlueter-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > ok yours is better, you win :) > > On 1/19/07, Thomas Fuchs <t.fuchs-moWQItti3gBl57MIdRCFDg@public.gmane.org> wrote: > > > > [''a'',''b'',''c''].include(''b'') --> true > > > > See http://prototypejs.org/api/enumerable/include > > > > Best, > > Thomas > > > > > > Am 19.01.2007 um 17:46 schrieb Kjell Bublitz: > > > > > > > > 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 > > > >-- Kjell -- www.m3nt0r.de --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Christophe Porteneuve
2007-Jan-19 17:51 UTC
Re: Prototype.js: Is there an "in_array"-like function?
Hey there, Kjell Bublitz a crit :> Nah.. both won.. include() is not strict.. with your example i couldThat''s right, and AFAIK, in_array isn''t either (at least by default). However, aside from issues with arrays containing such stuff as null, false and undefined, it''s not much of an issue, is it? -- Christophe Porteneuve aka TDD tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Thomas Fuchs
2007-Jan-19 21:06 UTC
Re: Prototype.js: Is there an "in_array"-like function?
Well, for example there''s 0 == '''' -> true So maybe not a bad idea at all, but i guess a new method is nicer for this. Best, Thomas Am 19.01.2007 um 18:51 schrieb Christophe Porteneuve:> > Hey there, > > Kjell Bublitz a écrit : >> Nah.. both won.. include() is not strict.. with your example i could > > That''s right, and AFAIK, in_array isn''t either (at least by default). > > However, aside from issues with arrays containing such stuff as > null, false and undefined, it''s not much of an issue, is it? > > -- > Christophe Porteneuve aka TDD > tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---