0 Replies Latest reply on Sep 12, 2009 3:38 AM by pepper4breakfast

    Array.filter: how to pass dynamic parameters?

    pepper4breakfast

      Hi, I've one Array of Objects:

       

      var obj1 ={a:10, b:"z", c:"jj"};
      var obj2 = {a:10, b:"r", c:"hh"};
      var obj3 = {a:14, b:"t", c:"jj"};
      var ar:Array = new Array(obj1,obj2,obj3);

       

      If I want to get a new Array containing all the "records" where a=10 I call the filter method:

       

      ar.filter(myMeth);

       

      and build a method to test the condition:

       

      function myMeth(e,i,a){

           return e.a==10;

      }

       

      This is good but not very practical because:

       

      1. you can't dinamically add the name of the property;

      2. you can't dinamically add the value of the property to test;

       

      You can't have:

      return a['prop'] = parameter;

       

      1. because it seems you can't add parameters;

      2. because it seems you can't add properties on the fly;

       

      Or better, there's a way, but one should create two class properties, one for the property, the other one for the value and assing them before starting the test. So, if you are in a for loop and you want to create n arrays based on different tests, you must first change the value of these class properties. Moreover you find yourself with class members set to a non-significant value at the and of the loop.

       

      Another solution (!) would be that of creating one method for each test...

       

      So, what am I missing? Is there any other way to pass parameters? I thought one could use Function.apply but it does not work. Delegates were an AS2 solution. Which is the AS3 solution?

       

      There are many others situations like this one, for example with array.some, vector.filter, etc... every situation where we have a callback and a function that accepts only one parameter...

       

      Could you please help me?   :-)