17

I have defined an interface :

export interface Student {
  name : String;
  section:String;
  phoneNumber:String;
}

i want to create an Object of an Object of type Student with studentId as key . i.e

studentDetails[studentId] should be of type Student;

studentDetails[studentId] : Student = [];

How can i implement this using inteface ?

Thanks in advance

9
  • Object of an object? can you explain that? Commented Sep 1, 2017 at 6:03
  • Question very unclear Commented Sep 1, 2017 at 6:05
  • 1
    i guess he is looking for a map operator on his object like {id: {name:..}} Commented Sep 1, 2017 at 6:05
  • do you mean some sort of array/map of student objects? Commented Sep 1, 2017 at 6:06
  • 1
    oh ok, so that would be Map<number/string,Student> depending if the studentId is a string or number Commented Sep 1, 2017 at 6:08

2 Answers 2

21

You can define another interface for StudentDetails like this:

export interface Student {
  name: string;
  section: string;
  phoneNumber: string;
}

export interface StudentDetails {
  [key: number]: Student; //Or string instead of number
}

And use it like this:

//Valid
let studentDetails: StudentDetails = {
  1: {
    name: 'Test Person1',
    section: 'Section 1',
    phoneNumber: '12345678'
  }
};

//Valid
studentDetails[2] = {
  name: 'Test Person 2',
  section: 'Section 2',
  phoneNumber: '87654321'
};

//Invalid (properties in UpperCase)
studentDetails[3] = {
  Name: 'Test Person 3',
  Section: 'Section 3',
  PhoneNumber: '52376724'
};

//Valid
let student: Student = studentDetails[2];

UPDATE

From your comment to my answer, I assume you want something like this instead:

export interface Student {
  id: number; // Added
  name: string;
  section: string;
  phoneNumber: string;
}

export interface StudentMap {
  [key: number]: Student;
}

let studentMap: StudentMap = {};
let studentDetails: Student[] = [
  {
    id: 56,
    name: 'Test Person1',
    section: 'Section 1',
    phoneNumber: '12345678'
  },
  {
    id: 175,
    name: 'Test Person3',
    section: 'Section 3',
    phoneNumber: '521398763'
  }
];

studentDetails.forEach((s: Student) => {
  studentMap[s.id] = s;
});

for (let i = 0; i < studentDetails.length; i++) {
  console.log(studentMap[studentDetails[i].id].name); // Test Person1, Test Person 3
}
Sign up to request clarification or add additional context in comments.

3 Comments

how can i use the above method to do something like : for(let i=0; i < this.sudentDetails.length; i++){ this.sudentMap[studentDetails[i].id].name = this.studentDetails[i].name } ?
The syntax you're mentioning is only for iterating on arrays. In this example studentDetails is an object. Use the following syntax to iterate on the properties of an object : for (var prop in obj) { console.log(obj.${prop} = ${obj[prop]}); }
@CruelEngine I think your question is wrong if you want to be able to do studentDetails[i] and not studentDetails[studentId]. That would indicate an Array instead of an Object. Instead studentMap would be the Object that studentDetails is in my answer. studentDetails would simply be of type Student[].
2

First Add a studetId to the interface :

export interface Student {
name : String;
section:String;
phoneNumber:String;
studentID : number;
 }

Then create a map of studentId with the student detail

  students : Student[];
  student1 : Student;

  var studentMap = new Map();

  this.students  = [
  {name : "abc", section :'A', phoneNumber : '123',studentID : 101 },
  {name : "xyz", section :'B', phoneNumber : '456',studentID : 102 },
  {name : "mno", section :'C', phoneNumber : '789',studentID : 103 },

    ];

 for (var item of this.students){
  studentMap.set (item.studentID ,item);
 }

 this.student1 = studentMap.get(101);

Please see this (Plunkr I have created )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.