title: Iterating a dataProvider in flex
body: <p>I'm wondering... I want to iterate through a dataProvider, in a Component which is based on a DropDownList.
  First thing, that it didn't work (it compiled, but never iterated), was:</p>
  
  <pre><code>var o:Object;
  for each (var o:Object in dataProvider)
  {
  }
  </code></pre>
  
  <p>I guess it didn't work because a IList doesn't provide Objects, or something someone would be able to explain easily.</p>
  
  <p>I tried something that looks horrible from a efficiency perspective, nonetheless it works. This is it:</p>
  
  <pre><code>for (var i:int = 0; i &lt; dataProvider.length; i++)
  {
     o = dataProvider.getItemAt(i);
  }
  </code></pre>
  
  <p>But it is so horrible that I felt tempted to ask here about another possible solution.</p>
  
  <p>UPDATE:</p>
  
  <p>I'll try to elaborate... I'm making (well, it is made already) a component that, being like a DropDownList, is bindable, not to an index (like selectedIndex="@{variable}"), but to a value of a variable inside of the ArrayCollection.</p>
  
  <p>Say, you have a dataProvider with two objects: <code>{a:'5', nmb:'five', blabla:'cinco'}</code> and <code>{a:'39', nmb:'thirty-nine', blabla:'treinta y nueve'}</code>.</p>
  
  <p>This component, if declared like this:</p>
  
  <pre><code>&lt;com:ddListN idxName="a" selectedN="@{val}" labelField="nmb"&gt;
  </code></pre>
  
  <p>Does use <code>val</code> to set/get the DropDownList to the proper index, comparing the value with the variable defined in idxName.</p>
  
  <p>Well, this is the entire code (is not that much):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;s:DropDownList xmlns:fx="http://ns.adobe.com/mxml/2009" 
                  xmlns:s="library://ns.adobe.com/flex/spark" 
                  xmlns:mx="library://ns.adobe.com/flex/mx"
                  change="ch()"&gt;
      &lt;fx:Declarations&gt;
      &lt;/fx:Declarations&gt;
      &lt;fx:Script&gt;
          &lt;![CDATA[
              private var _selectedN:String;
  
              public var idxName:String = 'n';
  
              [Bindable(event="changeSelected")]
              public function get selectedN():String
              {
                  return this.selectedItem[idxName];
              }
  
              public function set selectedN(v:String):void
              {
                  var o:Object;
  //              for each (var o:Object in dataProvider) @@
                  for (var i:int = 0; i &lt; this.dataProvider.length; i++)
                  {
                      o = dataProvider.getItemAt(i);
                      if (o[idxName] == v)
                      {
                          this.selectedIndex = i;
                          _selectedN = v;
                          dispatchEvent(new Event("changeSelected"));
                          return; 
                      }
  
                  }
                  this.selectedItem = null; // no seleccionar nada (@@?)
                  _selectedN = null;
                  dispatchEvent(new Event("changeSelected"));
              }
  
              private function ch():void
              {
                  _selectedN = this.selectedItem[idxName];
                  dispatchEvent(new Event("changeSelected"));
              }
          ]]&gt;
      &lt;/fx:Script&gt;
  &lt;/s:DropDownList&gt;
  </code></pre>