1

I have this object which has many elements such as a couple other objects within the array.

There are Two Arrays:

array of objects1:

enter image description here

Inside that is this:

Subform and Section

the hierarchy is:

[0]--
title
     |
     section [
             properties|
                       [[***target***]]
                                       |
                                       title
     ]
     subform [
             properties|
                       [[***target***]]
                                       |
                                       title
     ]
[1]--
     |
     section [
             properties|
                       [[***target***]]
                                       |
                                       title
     ]
     subform [
             properties|
                       [[***target***]]
                                       |
                                       title
     ]

What I'm trying to do is selectively PICK OUT the titles like so:

I instantiate private obj like so:

private obj = {
  data: [{
    subform: '',
    section: '',
    field: ''
  }]
};

and run a for/next loop to populate the obj OBJECT

    for (let i = 0; i < 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;
    }

What happens is, it goes around 1 TIME and comes back for "1" and says in the error console:

Cannot set property 'subform' of undefined.

the result I seek is simply this:

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'
  }]
};

Why is it DYING on the second time around? This is so simple... I'm using TYPESCRIPT

UPDATE FOR DAI:

Dai, I'm 99% there...

When implementing the code, I wrote the following enum.

export interface DataItem {
   subform: string;
   section: string;
   field: string;
};

export type ObjType = {
  data: DataItem[]
};

This is the import of the above

import {DataItem, ObjType} from '../../../services/datatables-integration-services/datatables-datatypes-enum';

and in the actual component.ts file I've put this:

 private readonly obj: ObjType = {
    data: []
 };

And I implemented the for/next loop as you said.

    for (let i = 0; i < 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;
    }

The below is the error I'm getting and to overcome that error I needed to change

This

  subform = this.dataTableJSON[i].subform.properties.title,
  section = this.dataTableJSON[i].section.properties.title,
  field   = this.dataTableJSON[i].title

To THIS

  subform: this.dataTableJSON[i].subform.properties.title,
  section: this.dataTableJSON[i].section.properties.title,
  field: this.dataTableJSON[i].title

Now testing

And it WORKS! BOOYAH!

Thank you Dai!

UPDATE FOR DAI:

I have an minor mistake:

The FINAL JSON needs to look like this

  {
    "data": [
      {
        field: "Social Security number"
        required: true
        section: "Employee Information"
        subform: "Personal Information"
      },
      {
        field: "Country of issuance"
        required: true
        section: "Eligibility Information"
        subform: "Employment Eligibility"
      }
    ]
  }

Sorry, Dai... thanks again

enter image description here

enter image description here

5
  • You can only assign to a member of an indexed-element in an array if a correctly-typed object instance exists at that index. Commented Aug 31, 2020 at 4:35
  • OK, great, telling me that is fantastic... how do I get what I want and fix it, please! Also, this may in-fact be dynamic. Meaning everything after data[] may not be section.title, subform.title, or title. I tried push but that doesn't result in anything except and {} as 0, the 1: title1, 2: title 2 NOT inside the {} Commented Aug 31, 2020 at 4:36
  • My comment explains what's wrong with your code. If you need help understanding what I wrote then please ask to me elaborate - otherwise I feel that my comment does explain how to fix it. Please don't be sarcastic. Commented Aug 31, 2020 at 4:37
  • Dai, I'm sorry you took it that way. I thought you understood that if you make a suggestion, it's a given that you'd provide a solution that I can VOTE for you Commented Aug 31, 2020 at 4:38
  • I didn't provide a full answer in my comment - it was more of a hint or a nudge in the right direction. But I've provided a fuller answer now. Commented Aug 31, 2020 at 4:40

1 Answer 1

1

Change this:

    for (let i = 0; i < 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;
    }

To this:

    // Put these `type` declarations somewhere appropriate in your codebase:

    interface DataItem = {
        subform: string;
        section: string;
        field  : string;  
    };

    type ObjType = {
        data: DataItem[]
    };

    // Change your `obj` member to this:
    private readonly obj: ObjType = {
        data: []
    };

    // And change your for loop to this:
    for (let i = 0; i < 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;
    }

You can also do this (which is syntactically shorter, but hides type information which may confuse people new to TypeScript and JavaScript who may not understand what's going-on):

    for (let i = 0; i < this.dataTableJSON.length; i++) {
        
        this.obj.data[i] = {
            subform: this.dataTableJSON[i].subform.properties.title,
            section: this.dataTableJSON[i].section.properties.title,
            field  : this.dataTableJSON[i].title;
        };
    }

This can be syntactically simplified to this:

    for (let i = 0; i < this.dataTableJSON.length; i++) {
        const d = this.dataTableJSON[i];
        this.obj.data[i] = {
            subform: d.subform.properties.title,
            section: d.section.properties.title,
            field  : d.title;
        };
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Let me try it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.