I have this object which has many elements such as a couple other objects within the array.
There are Two Arrays:
array of objects1:
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


