title: Iterating through an array in angular2 typescript
body: <p>Am new to typescript and angular2 and learnining this and i have this issue. I have a form that i would like to show the results of an array of items but these items keep changing everytime so i dont now the actual index of the returned results.</p>
  
  <p>This is the case</p>
  
  <p>I have this form.html</p>
  
  <pre><code>&lt;span style="color:red"&gt;{{usererror.username}}  &lt;/span&gt;
  ...other items here
  &lt;span style="color:red"&gt;{{usererror.password}}  &lt;/span&gt;
  </code></pre>
  
  <p>Now in the component</p>
  
  <pre><code>export class LoginComponent implements OnInit{
   public usererror={
      username:"",
      password:""
    }
  
    //this function is called after a post request
  onLoginError(error:any){
  
  let message= JSON.parse( error._body);
    console.log(error._body); //first log
    console.log(message)  //second log
  
  
  }
  
  }
  </code></pre>
  
  <p>As from the above function the first log returns an array of the form</p>
  
  <pre><code>[{"field":"username","message":"Username cannot be blank."},
    {"field":"password","message":"Password cannot be blank."}]
  </code></pre>
  
  <p>Now i would like to assign the username and password variables in the above usererror so that they can be displayed</p>
  
  <p>So something like this</p>
  
  <pre><code>onLoginError(error:any){
  
  let message= JSON.parse( error._body);
     let message= JSON.parse( error._body);
  
      for (var i=0; i&lt;message.length; i++){
       this.usererror.message[i].field = message[i].message;  //this fails since the 
           //usererror doesnt have (message[i].field) property
  
       }
   }
  </code></pre>
  
  <p>sometimes the returned results dont have a username field and sometimes the password field. How do i go about this, am still new to angular2 and typescript</p>