I've created an interface that looks like this:
export interface testInterface
{
value1: string;
value2: string[];
SubValue: [{}]
}
Everything works fine as I fill an array of type testInterface, which is bound to a dropdown list. Now, I want the dropdown list to display "Pick One" as its first item. Here's what I did:
default: testInterface[] = [];
this.default.value1 = "----Please Select the Sub-Industry---";
this.default.value2 = [];
this.default.SubValue = [];
However, I encountered an error on the line referencing subValue: Type 'undefined[]' is not assignable to type '[{ value1: string; value2: string[]; value3...'. Property '0' is missing in type 'undefined[]'.
Why can't I just say dropdownArray[0].push=this.default???
Could someone please explain why this doesn't work? The issue might be obvious to some, but I'm still learning!
ACTUAL CODE:
// How I populate the bound array:
subsDataofSelectedSector: ISubIndustry[] = [];
if (response.SubIndustries.length > 0) {
for (i = 0; i < response.SubIndustries.length; i++) {
this.subsDataofSelectedSector.push(response.SubIndustries[i]);
}
}
// The interface
export interface ISubIndustry
{
IndustrySector: string;
isSelected: string;
dataSubjectCategories: string[];
dataTypeCategories: string[];
SubIndustries: [{}]
}
// The default array
this.defaultSub.dataSubjectCategories = [];
this.defaultSub.dataTypeCategories = [];
this.defaultSub.IndustrySector = "----Please Select the Sub-Industry---";
this.defaultSub.SubIndustries = [];
this.defaultSub.isSelected = "true";
// Now I'd like to do the following and add the rest after index 0
this.subsDataofSelectedSector[0].push(this.defaultSub)
Here is a snippet from the JSON file I need to add to the array:
{
"IndustrySector": "Other Services Activities",
"isSelected": "false",
"dataSubjectCategories": ["DS.Employees", "DS.Collaborators", "DS.Legal Person", "DS.Natural Person"],
"dataTypeCategories": ["Personal Data"],
"SubIndustries": [
{
"IndustrySector": "Activities of Membership Organisations",
"isSelected": "false",
"dataSubjectCategories": [],
"dataTypeCategories": [],
"SubIndustries": [
{
"IndustrySector": "Activities of Other Membership Organisations",
"isSelected": "false",
"dataSubjectCategories": [],
"dataTypeCategories": [],
"SubIndustries": [
{
As you can see, each value can have subvalues and each subvalue can have further subvalues. Hence, I defined it as an array of objects.