title: Initializing array of objects - Angular
body: <p>I have an array of objects and when I try to access to it, I get an error saying:</p>
  
  <blockquote>
    <p>TypeError: Cannot set property 'ID' of undefined</p>
  </blockquote>
  
  <p>My code is the following:</p>
  
  <pre><code>export class Carimplements OnInit {
    pieces: Piece[] = [];
  
    test(pos){
      this.pieces[pos].ID = "test";
    }
  }
  </code></pre>
  
  <p>being Piece an object</p>
  
  <pre><code>export class Piece{
      ID: string;
      doors: string;
  }
  </code></pre>
  
  <p>I call to <code>test(pos)</code> from the HTML with a valid position.</p>
  
  <p>I guess that I am trying to access to the position X of an array that has not been initialized. How could I do it? Is it possible to create a constructor?</p>