find next element in an array?

Showing 1-6 of 6 messages
find next element in an array? Walter Lee Davis 1/31/11 2:12 PM
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

Re: find next element in an array? Marc 2/1/11 12:37 AM
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
Re: find next element in an array? T.J. Crowder 2/1/11 3:35 AM
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
Re: [Proto-Scripty] Re: find next element in an array? Bertilo Wennergren 2/1/11 3:51 AM
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

Re: [Proto-Scripty] Re: find next element in an array? Walter Lee Davis 2/1/11 5:49 AM
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-scriptaculous+unsubscribe@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en
> .
>

Re: find next element in an array? T.J. Crowder 2/1/11 7:42 AM
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