using Array.each method of prototype library

T

tukaopaleye

Hi,

I am trying to use the prototype.js library and trying to substitute:

for (var index = 0; index < elementArray.length; ++index) {
var e = elementArray[index];
if (e.id.include(controlId))
{
return e;
}
}


which i want to express as:

elementArray.each(function(elem){
if (elem.id.include(controlId))
{

return elem;
}
});

It seems that the return element is ignored and the loop just
continues even if the condition is evaluated to true. in the
documentation, http://www.prototypejs.org/api/enumerable/each it
says that return is usable but I am not clear how, I am tringto avaid
the deprecated $continue and $break...

TIA,
 
G

Geoffrey Summerhayes

Hi,

I am trying to use the prototype.js library and trying to substitute:

for (var index = 0; index < elementArray.length; ++index) {
var e = elementArray[index];
if (e.id.include(controlId))
{
return e;
}
}

which i want to express as:

elementArray.each(function(elem){
if (elem.id.include(controlId))
{

return elem;
}
});

I don't use prototype, but isn't this
just the find() method?
 
D

David Mark

Hi,

I am trying to use the prototype.js library and trying to substitute:

for (var index = 0; index < elementArray.length; ++index) {
var e = elementArray[index];
if (e.id.include(controlId))
{
return e;
}
}

which i want to express as:

elementArray.each(function(elem){
if (elem.id.include(controlId))
{

return elem;
}
});

Why? Even if this were the right logic, it would be considerably
slower than the original code. You save a few characters, but you
have to add an 80K script to do it.
It seems that the return element is ignored and the loop just
continues even if the condition is evaluated to true. in the
Right.

documentation,http://www.prototypejs.org/api/enumerable/each it
says that return is usable but I am not clear how, I am tringto avaid
the deprecated $continue and $break...

I don't see where it says that.
 
R

RobG

Hi,

I am trying to use the prototype.js library and trying to substitute:

for (var index = 0; index < elementArray.length; ++index) {
var e = elementArray[index];
if (e.id.include(controlId))
{
return e;
}
}


which i want to express as:

elementArray.each(function(elem){
if (elem.id.include(controlId))
{
return elem;
}
});

It seems that the return element is ignored and the loop just
continues even if the condition is evaluated to true.

In Prototype.js, the each method simply calls the supplied function
once for each element in the array, passing the array element's value
as the sole parameter. The original array is unmodified and is
returned.

$break and $continue were there to control execution of the supplied
function, they did not stop the each method from looping over all the
elements.
in the
documentation, http://www.prototypejs.org/api/enumerable/each it
says that return is usable but I am not clear how, I am tringto avaid
the deprecated $continue and $break...

It returns the original, unmodified array. It seems you are concerned
about the amount of code you have to write - but that should not be
your primary concern. Using each() is almost certainly slower than a
for loop, but if you want to emulate each() and not write so much
code, consider using do..while:

var e, i=0, j=elementArray.length;
do {
e = elementArray[i++];
/* do stuff with e */
} while (i < j)

which has the pleasant side-effect of being about 6 times faster in
Firefox than the equivalent Prototype.js each() method and better than
10 times faster in IE. A normal for loop is about 5 times faster.
You may also note that Prototype clobbers the native
Array.prototype.each method if it exists, not very nice.
 
T

tuka

Thanks for your explanation.

I am also heeding yours and the others suggestions to use more
efficient routines which I will consider. I will certainly look into
the while loop. The benchmarks are also helpful.

Thanks again.
tuka

In Prototype.js, the each method simply calls the supplied function
once for each element in the array, passing the array element's value
as the sole parameter. The original array is unmodified and is
returned.

$break and $continue were there to control execution of the supplied
function, they did not stop the each method from looping over all the
elements.
in the
documentation,http://www.prototypejs.org/api/enumerable/each it
says that return is usable but I am not clear how, I am tringto avaid
the deprecated $continue and $break...

It returns the original, unmodified array. It seems you are concerned
about the amount of code you have to write - but that should not be
your primary concern. Using each() is almost certainly slower than a
for loop, but if you want to emulate each() and not write so much
code, consider using do..while:

var e, i=0, j=elementArray.length;
do {
e = elementArray[i++];
/* do stuff with e */
} while (i < j)

which has the pleasant side-effect of being about 6 times faster in
Firefox than the equivalent Prototype.js each() method and better than
10 times faster in IE. A normal for loop is about 5 times faster.
You may also note that Prototype clobbers the native
Array.prototype.each method if it exists, not very nice.
 
D

David Mark

[snip]
In Prototype.js, the each method simply calls the supplied function
once for each element in the array, passing the array element's value
as the sole parameter.  The original array is unmodified and is

Like the JS 1.6 forEach method that it apes, it passes the often
unused index as a second parameter.

[snip]
You may also note that Prototype clobbers the native
Array.prototype.each method if it exists, not very nice.

Are you thinking of forEach?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top