How do I add elements to an array of objects in Typescript? According to what I think it should be as follows but for some reason it doesn't work.
OBJECT
conSubNet: Port[] = [{
ip: '',
pu: '',
uu: '',
bc: '',
mm: '',
host: '',
reqHost: ''
}];
LINE WHERE I ADD A DATA
this.conSubNet[z].ip = this.octOne + ' . ' + this.octTwo + ' . ' + this.octThree + ' . ' + this.octFour;
It doesn't work because
Select the existing item in the index
z
. If such an index does not exist, it returnsundefined
. Yundefined
is not an object and does not support access to its inner elements using.
.The solution is to assign an object to position
[z]
. Change your assignment toNote that if an element already exists at that position , that code replaces that element. If you are not sure that the array is empty, you should check it first: