find next element in an array?

17 views
Skip to first unread message

Walter Lee Davis

unread,
Jan 31, 2011, 5:12:11 PM1/31/11
to prototype-s...@googlegroups.com
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

Marc

unread,
Feb 1, 2011, 3:37:21 AM2/1/11
to Prototype & script.aculo.us
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

T.J. Crowder

unread,
Feb 1, 2011, 6:35:11 AM2/1/11
to Prototype & script.aculo.us
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

Bertilo Wennergren

unread,
Feb 1, 2011, 6:51:19 AM2/1/11
to prototype-s...@googlegroups.com
On Tue, Feb 1, 2011 at 12:35, T.J. Crowder <t...@crowdersoftware.com> wrote:

> (now that `$break` has been deprecated).

"$break" has been deprecated? Where is that documented?
What are we supposed to use instead?

--
Bertilo Wennergren
bert...@gmail.com http://bertilow.com

Walter Lee Davis

unread,
Feb 1, 2011, 8:49:19 AM2/1/11
to prototype-s...@googlegroups.com
Yes, exactly! I knew it was in there somewhere, but I couldn't find
the ref.

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-s...@googlegroups.com
> .
> To unsubscribe from this group, send email to prototype-scripta...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en
> .
>

T.J. Crowder

unread,
Feb 1, 2011, 10:42:54 AM2/1/11
to Prototype & script.aculo.us
Hi,

> "$break" has been deprecated?

Yup.

> Where is that documented?

I don't know, I only know because Tobie mentioned it to me once. I'm
sure it's mentioned in a thread here somewhere. You'll note it's
disappeared from the documentation. ECMAScript 5th edition's `forEach`
(which is pretty much equivalent) doesn't have the concept of breaking
early (you have to use `every` or `some` if you want to break early --
it's done via a return value).

> What are we supposed to use instead?

Enumerable has `all`, which is very like ECMAScript5's `every` in that
it stops when you return `false` out of the callback (but ECMAScript5
defines that the callback gets all three arguments; I don't think
Enumerable makes that guarantee). Enumerable also has `any` which
(like ECMAScript5's `some`) stops when you return `true` out of the
callback. In both cases, using a return value is probably cleaner and
less resource-intensive than throwing an exception. But if you really
like throwing `$break`, you can define it yourself if/when Prototype
actually drops support for it (1.7 still seems to have it, but I bet
the next major version aligns with ECMAScript5). It's easily done. :-)

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

On Feb 1, 11:51 am, Bertilo Wennergren <berti...@gmail.com> wrote:
> On Tue, Feb 1, 2011 at 12:35, T.J. Crowder <t...@crowdersoftware.com> wrote:
> > (now that `$break` has been deprecated).
>
> "$break" has been deprecated? Where is that documented?
> What are we supposed to use instead?
>
> --
> Bertilo Wennergren
> berti...@gmail.comhttp://bertilow.com
Reply all
Reply to author
Forward
0 new messages