title: How to push selected objects from an array of objects into one array of objects
body: <p>I have this object which has many elements such as a couple other objects within the array.</p>
  <p>There are Two Arrays:</p>
  <p>array of objects1:</p>
  <p><a href="https://i.stack.imgur.com/WJDZQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WJDZQ.png" alt="enter image description here" /></a></p>
  <p>Inside that is this:</p>
  <p>Subform and Section</p>
  <p>the hierarchy is:</p>
  <pre><code>[0]--
  title
       |
       section [
               properties|
                         [[***target***]]
                                         |
                                         title
       ]
       subform [
               properties|
                         [[***target***]]
                                         |
                                         title
       ]
  [1]--
       |
       section [
               properties|
                         [[***target***]]
                                         |
                                         title
       ]
       subform [
               properties|
                         [[***target***]]
                                         |
                                         title
       ]
  </code></pre>
  <p>What I'm trying to do is selectively PICK OUT the titles like so:</p>
  <p>I instantiate private obj like so:</p>
  <pre><code>private obj = {
    data: [{
      subform: '',
      section: '',
      field: ''
    }]
  };
  </code></pre>
  <p>and run a for/next loop to populate the obj OBJECT</p>
  <pre><code>    for (let i = 0; i &lt; this.dataTableJSON.length; i++) {
        this.obj.data[i].subform = this.dataTableJSON[i].subform.properties.title;
        this.obj.data[i].section = this.dataTableJSON[i].section.properties.title;
        this.obj.data[i].field = this.dataTableJSON[i].title;
      }
  </code></pre>
  <p>What happens is, it goes around 1 TIME and comes back for &quot;1&quot; and says in the error console:</p>
  <p>Cannot set property 'subform' of undefined.</p>
  <p>the result I seek is simply this:</p>
  <pre><code>private obj = {
    data: [{
      subform: 'Title 1',
      section: 'Section Title 1',
      field: 'Field Title 1'
    },{
      subform: 'Title 2',
      section: 'Section Title 2',
      field: 'Field Title 2'
    }]
  };
  </code></pre>
  <p>Why is it DYING on the second time around? This is so simple... I'm using TYPESCRIPT</p>
  <p>UPDATE FOR DAI:</p>
  <p>Dai, I'm 99% there...</p>
  <p>When implementing the code, I wrote the following enum.</p>
  <pre><code>export interface DataItem {
     subform: string;
     section: string;
     field: string;
  };
  
  export type ObjType = {
    data: DataItem[]
  };
  </code></pre>
  <p>This is the import of the above</p>
  <pre><code>import {DataItem, ObjType} from '../../../services/datatables-integration-services/datatables-datatypes-enum';
  </code></pre>
  <p>and in the actual component.ts file I've put this:</p>
  <pre><code> private readonly obj: ObjType = {
      data: []
   };
  </code></pre>
  <p>And I implemented the for/next loop as you said.</p>
  <pre><code>    for (let i = 0; i &lt; this.dataTableJSON.length; i++) {
  
        const dataCopy: DataItem = {
          subform = this.dataTableJSON[i].subform.properties.title,
          section = this.dataTableJSON[i].section.properties.title,
          field   = this.dataTableJSON[i].title
        };
  
        this.obj.data[i] = dataCopy;
      }
  </code></pre>
  <p>The below is the error I'm getting and to overcome that error I needed to change</p>
  <p><strong>This</strong></p>
  <pre><code>  subform = this.dataTableJSON[i].subform.properties.title,
    section = this.dataTableJSON[i].section.properties.title,
    field   = this.dataTableJSON[i].title
  </code></pre>
  <p><strong>To THIS</strong></p>
  <pre><code>  subform: this.dataTableJSON[i].subform.properties.title,
    section: this.dataTableJSON[i].section.properties.title,
    field: this.dataTableJSON[i].title
  </code></pre>
  <p><strong>Now testing</strong></p>
  <p>And it WORKS! BOOYAH!</p>
  <p>Thank you Dai!</p>
  <p>UPDATE FOR DAI:</p>
  <p>I have an minor mistake:</p>
  <p>The FINAL JSON needs to look like this</p>
  <pre><code>  {
      &quot;data&quot;: [
        {
          field: &quot;Social Security number&quot;
          required: true
          section: &quot;Employee Information&quot;
          subform: &quot;Personal Information&quot;
        },
        {
          field: &quot;Country of issuance&quot;
          required: true
          section: &quot;Eligibility Information&quot;
          subform: &quot;Employment Eligibility&quot;
        }
      ]
    }
  </code></pre>
  <p>Sorry, Dai... thanks again</p>
  <p><a href="https://i.stack.imgur.com/cQtAL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cQtAL.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/ezmPH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ezmPH.png" alt="enter image description here" /></a></p>