I have a JSON object named allDepartments, which consists of an array of custom data type contactItem:
export interface contactItem {
id: number;
position: string;
name: string;
email: string;
extension: number;
phone: Text;
department: string;
}
public allDepartments: contactItem[]; //interface defined in contactService
This is how allDepartments looks like:
[
{
"id": "1",
"position": "Director of Pathology and Laboratory Medicine",
"name": "Dr. Niall Swan",
"email": "[email protected]",
"extension": "4798",
"phone": "012214798",
"department": "General"
},
{
"id": "2",
"position": "Laboratory Manager",
"name": "Donal Murphy",
"email": "[email protected]",
"extension": "4510",
"phone": "012124510",
"department": "General"
},
{
"id": "4",
"position": "Laboratory Manager",
"name": "Donal Murphy",
"email": "[email protected]",
"extension": "4510",
"phone": "012124510",
"department": "General"
},....
If the department key in a contactItem is set to biochemistry, I want to add that contactItem object to an array called biochemistry:
public biochemistry: contactItem[];
// Iterate through the array and assign biochemistry contacts
console.log("contacts.page.ts: trying to get biochemistry data...");
for (var contact in this.allDepartments){
console.log("contact = " + this.allDepartments[contact].department);
if (this.allDepartments[contact].department == "biochemistry"){
this.biochemistry.push(this.allDepartments[contact]);
console.log("biochem object: " + this.allDepartments[contact]); // Array of objects
}
}
An exception is occurring:
TypeError: undefined is not an object (evaluating 'this.biochemistry.push')
The syntax for pushing an object to a TypeScript array seems correct, so I am unsure about the issue. Any insights would be appreciated.