How to recursively dive through deeply nested typescript models? And map those deep paths as an object [key: value]

馋奶兔 提交于 2020-06-16 02:25:05

问题


I've a challenge.

How to recursively dive through deeply nested typescript models? And map those deep paths as an object [key: value].

where key - is the column name or an identifiable name & value is the path (depth ) traversed.

what I mean by depth is this someObject.level1.level2.level3.level4... Like that.

Imagine I have a couple of Typescript Interfaces (all external files).

One Interface Pointing to another then the other & so On.

Eg:


export interface University {
  name: string;
  type: string;
  locationDetails: Location;
  revenue: Revenue;
  departments: Departments[];
  collegesUnder: Colleges[];
  status: boolean;
}

Here is the interface describing Location

export interface Location {
  street: string;
  state: string;
  postCode: number;
}

Here is the interface describing a Department


export interface Departments {
  name: string;
  category: string;
  coursesOffered: Courses[];
}

Here is the interface describing a College

export interface College {
  collegeName: string;
  collegeLocation: Location;
  coursesOffered: Courses[];
  studentsEnrolled: Students[];
}

Here is the interface describing a Student

export interface Student {
  studentName: string;
  department: Department;
  course: Course;
  age: number;
  address: string;
  otherInfo: OtherInfo;
}

Here is the interface describing a Course

export interface Course {
  name: string;
  classifiedUnder: string;
  subCourses: Course[];
}

So as you can there are a bunch of typescript files. All of them are separate files & are named by the convention interfaceName.model.ts. interfaceName corresponds to each Interface Identifier Name. The frontent is being developed using Angular.

I will be getting json data from a server which contains the infos of universities. & all these datas will be shown as a table(without any nesting).

On top of each headers I will have an input field & as the use types in the table filters out.

In order to search deeply I will have to access the deeply nested paths of these models. And I will have to do that separately for each type of search requests.

& whenever some property is added newly in any of these interfaces, I will have to come & make changes accordingly.

So I come up with a solution:

to map each column name with the depth of the object:

For example if the user searches in the column of Student Name:

return universities.filter(university => {
retrurn university.colleges.some(college => {
return college.students.some(student => {
return student.name
})
})
})

So my idea is to map each column name with each depths so that I can access it directly:

like this:

searchMap = {
universityName: university.name,
universityType: university.type;
universityLocationStreet: university.location.street;
universityLocationState: university.location.state;
universityLocationPostcode: university.location.postcode;

... Like so...

studentsName: university[i].colleges[j].students[k].student.name
// see how the nesting goes on... 
}

So the point is that if any changes occur in the typescript.model.ts files (in future if they make any changes to the properties) I want to manage it automatically.

So I want to find a solution to recursively crawl through these typescript interfaces (which are all external & will be imported inside the type definitions) & make a searchMap (depth map) object out of it.

来源:https://stackoverflow.com/questions/60868988/how-to-recursively-dive-through-deeply-nested-typescript-models-and-map-those-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!