> If you are searching for an Array.filter...

There's also Enumerable.inject (Array mixes in Enumerable):
http://www.prototypejs.org/api/enumerable/inject

I wouldn't want to see each() get an additional optional predicate
parameter just because I suspect the majority use case by far doesn't
use a predicate, so I wouldn't want to see it doing a lot of
unnecessary tests (e.g., "if (!predicate || predicate(entry))" to see
whether to iterate the entry).

Another approach, if you want to avoid creating an interim array with
filter/inject/grep/etc., is to use a predicate iterator factory:

    function createFilter(predicate, iterator)
    {
        return function(x) {
            if (predicate(x)) {
                iterator(x);
            }
        };
    }

This costs a function creation rather than an array creation; depends
on what your needs are.  You can use it inline:

    var     a;
    a = [1, "two", 3, 4, "five"];
    a.each(createFilter(Object.isString, function(x) {
        alert("Filter: " + x);
    }));
    // Alerts "Filter: two" and then "Filter: five"

...or create it up front and reuse it, if that's more appropriate:

    var     a;
    var     myNiftyFilter;
    // ...
    myNiftyFilter = createFilter(Object.isString, function(x) {
        alert("Filter: " + x);
    });
    // ...
    a = [1, "two", 3, 4, "five"];
    a.each(myNiftyFilter);
    // Alerts "Filter: two" and then "Filter: five"

FWIW,
--
T.J. Crowder
tj / crowder software / com

On Sep 12, 10:03 am, Josi <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I would do that with an Array.filter().each().
> Using two functions as you want to do is too specific to your problem
> in my opinion.
>
> If you are searching for an Array.filter, you can use the extension
> which I've posted on my blog some days ago:
>
> http://michael.josi.de/2008/09/04/update-arrayfilter/
>
> EMoreth wrote:
> > Working with prototype from a long time ago I always need a function
> > that could do something just like the Strongly Type languages, as Java
> > or many others, that is a for ( ObjectClass object in
> > arrayLikeStructure ) to get all objects from that array that are from
> > the class ObjectClass and do something...
>
> > Since Javascript does not have this kind of Typed Class its is very
> > hard and annoying to do such thing... I recently tought two ways that
> > could make it work...
>
> > First (and simpler) is a foreach function that applyes on Enumerable
> > items and Accepts two functions.. the first one is a boolean that will
> > tell the code when the object is the class/type that I want, and the
> > second function should be the action at real... to apply in that
> > object. Something like :
>
> > myArray.foreach(function(obj){ return obj.className ==
> > 'MyProtoClass'}, function(obj) { obj.doStuff() })
>
> > The second one is just a little different (more Java-Like)... in this
> > case we would need a previous custom method on all objects that would
> > say if this one matches a defined parameter or not... something like
>
> > a.className = 'MyClass';
> > b.className = 'MyProtoClass';
>
> > a.isMyObject = function(check) { return this.className == check }
> > b.isMyObject = function(check) { return this.className == check }
>
> > myArray.foreach( 'MyProtoClass', function(obj) { obj.doStuff() })
>
> > and it would only apply on b.doStuff()
>
> > I cant tell if this would have the same utility for everyone that
> > would have for me... But i thing that would be very good for people
> > who are used to other Typed Languages...
>
> > Please tell me what you think about that..
>
> > EMoreth
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to