Home > Net >  How to fetch array data in angular
How to fetch array data in angular

Time:08-06

This is how i assigned my model in a variable in my component. Here i want to fetch data by calling familyHistoryModel.patientMedicalFamilyHistoryDiseases.diseaseID but this gets error unable to call diseaseID like that is there any alternative way to access diseaseID data by calling familyHistoryModel.patientMedicalFamilyHistoryDiseases.required code to fetch diseaseId data

 export class FamilyHistoryModelComponent implements OnInit {
      familyHistoryModel: PatientMedicalFamilyHistoryModels;

below code is my model.

export class PatientMedicalFamilyHistoryModels {
    id?: number = 0;
    patientID: number = null;
    firstName: string = '';
    lastName: string = '';
    genderID: number;
    gender: string = '';
    dob: Date;
    relationshipID: number;
    relationShipName: string = '';
    dateOfDeath: Date;
    causeOfDeath: string = '';
    observation: string = '';
    patientMedicalFamilyHistoryDiseases?: PatientMedicalFamilyHistoryDiseaseModel[];
}

export class PatientMedicalFamilyHistoryDiseaseModel {
    id?: number = null;
    patientID?: number = null;
    medicalFamilyHistoryId?: number = null;
    diseaseID?: number = null;
    diseaseStatus?: boolean = false;
    ageOfDiagnosis?: number = null;
    diseaseName?: string =null;
}

CodePudding user response:

So when you are having the data in an Array and you want to get the ID(s) from the array you need to answer the question whether you want an ID of a specific item in the array, or you want the list of all IDs from this array.

If you want the ID of a specific item, you can get it using index of the item:

const id: string = this.familyHistoryModel.patientMedicalFamilyHistoryDiseases[0].diseaseID

However if you want the list of IDs for all items, you want to use a map to map items to their ids, as follows:

const ids: string[] = this.familyHistoryModel.patientMedicalFamilyHistoryDiseases.map(disease => disease.diseaseID)

CodePudding user response:

Did you try this?

this.familyHistoryModel.patientMedicalFamilyHistoryDiseases.map(x => x.diseaseID);

  • Related