Hi,

> I could swear there used to be an operator that worked over an  
> enumerable and maintained an index element as you went...

As Marc's pointed out that `each` does have the index as the second
parameter. But using `each` for this introduces unnecessary function
creation, function calls, and (if you want to stop once you've found
the target element) an avoidable try/catch block and conditional (now
that `$break` has been deprecated).

Fortunately, there's a better way. :-) `Array#indexOf`[1]:

var list = $$('.foo'),
    index,
    nextElement;
index = list.indexOf(targetElement);
if (index >= 0) {
    ++index;
    nextElement = list[index >= list.length ? 0 : index];
}

Prototype adds `Array#indexOf` to implementations that don't have it
natively.

[1] http://api.prototypejs.org/language/Array/prototype/indexOf/

HTH,
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com

On Feb 1, 8:37 am, Marc <marc.heilig...@gmail.com> wrote:
> The function you pass to each has a second optional index param:
>
> $$('.foo').each(function(elem, index) {
>   elem.update('I am element ' + index);
>
> });
>
> It's not all that clear in the docs but it's mentioned for the
> iterator param 
> here:http://api.prototypejs.org/language/Enumerable/prototype/each/
>
> Is that wat you were looking for?
>
> -- Marc
>
> On Feb 1, 12:12 am, Walter Lee Davis <wa...@wdstudio.com> wrote:
>
>
>
>
>
>
>
> > I have an extended array, result of $$('.foo'). Given a single member  
> > of that array as an object, how do I find the next element in that  
> > array, or the first if the next would wrap?
>
> > I could swear there used to be an operator that worked over an  
> > enumerable and maintained an index element as you went, so you could  
> > reference by position within the enumerable, but I can't find it by  
> > hunting in the documentation. Can anyone help me out?
>
> > Thanks,
>
> > Walter

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.

Reply via email to