English 中文(简体)
Flex looping through object
原标题:

Im trying to extend the flex ArrayCollection to be able to search for an object containing specific data and give it back.

Here is my function:

public function getItemContaining(value: String): Object {                      
          //Loop through the collection         
          for each(var i: Object in this) {                             
            //Loop through fields                               
            for(var j: String in i) {                   
                //If field value is equal to input value
                if(i[j] == value) {
                    return i;

                }
            }
        }
    //If not found
    return null;
    }

Problem is j is always null so the second loop never works. So I read flex loop descriptions and actually it should work just fine. What can possibly be the problem?

问题回答

Try it like this:

for (var name:String in myObject){
  trace(name + ":" + myObject[name];
}

Okay that was actually the same you were doing. The error must be in this line:

for each(var i: Object in this) {

Try using this:

for each(var i: Object in this.source) {

My first instinct would be to have a look at data type. You re setting up a loop declaring j:String and the symptom is that j is always null. This suggests to me that Flex is failing to interpret the elements of i as strings. If Flex only recognizes the elements of i as Objects (because all Strings are Objects, and Objects are the lowest common denominator), it would return null for j:String.

Try this for your inner loop:

for(var j: Object in i) {                   
    //If field value is equal to input value
    if(i[j] is String && (i[j] as String) == value) {
        return i;
    }
}

if you are using ArrayCollection as your datasource, you should look at using the IViewCursor interface. You can supply a custom compare function, or supply the fields top compare to. This interface is well documented with examples in adobe/livedocs

var _cursor:IViewCursor;
var _idSortField:SortField;
var _idSort:Sort = new Sort();
_idSortField = new SortField();
_idSortField.compareFunction = this.myCompareFunction; 
_idSort.fields = [_idSortField];
myArrayCollection.sort = _idSort;
myArrayCollection.refresh();
_cursor = myArrayCollection.createCursor();
if (_cursor.findAny(search))
   return _cursor;

if you are search for a value in a specific property, then its even easier. Here s the link to adobe livedocs on this topic





相关问题
Disable button tooltip in AS3

I want to disable the tooltip on certain buttons. The tooltip manager seems to be an all or nothing solution. Is it possible to disable the tooltip for just one or two buttons?

Multiple Remote call made simultenously

I was making multiple remote calls and they are done sequentially and when I am getting a result event back it s triggering calls to all the methods with ResultEvent as an argument . I am supposed to ...

Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

Clearing RSL in Cache

I have built a flex application which has a "main" project and it is assosciated with a few RSL s which are loaded and cached once i run my "main" application. The problem i am facing is that the ...

What s a good way of deserializing data into mock objects?

I m writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I d prefer not to generate the data in code like this: var mockData =...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

热门标签