I have getting a json array from a api.
[
{
id: 1,
parentId: null,
name: 'category 1'
},
{
id: 2,
parentId: null,
name: 'category 2'
}
]
so after getting this data i am using this id and call a api to get children.
[
{
id: 11,
parentId: null,
name: 'category 1 child 1'
},
{
id: 12,
parentId: null,
name: 'category 1 child 2'
}
]
further i am these children id to get children.
[
{
id: 21,
parentId: null,
name: 'child 1 sub child 1'
},
{
id: 22,
parentId: null,
name: 'child 1 sub child 2'
}
]
so every child have their own children.final what i want.
[
{
id: 1,
parentId: null,
name: 'category 1',
children: [
{
id: 11,
parentId: null,
name: 'category 1 child 1',
[
{
id: 11,
parentId: null,
name: 'category 1 child 1',
children: // further children
},
{
id: 12,
parentId: null,
name: 'category 1 child 2',
children: // further children
}
]
},
{
id: 12,
parentId: null,
name: 'category 1 child 2',
children: // same as above
}
]
},
{
id: 2,
parentId: null,
name: 'category 2'
}
]
what I am trying to achieve this here is my Category and Children Interface and models
export interface ICategory {
id?: any;
parentId?: number;
name?: string;
children: Children[]
}
export class Category implements ICategory {
public id?: any;
public parentId?: number;
public name?: string;
public children: Children[]
constructor(data: ICategory) {
Object.assign(this, data);
}
}
export interface IChildren {
id?: any;
parentId?: number;
name?: string;
children: Children[]
}
export class Children implements IChildren {
public id?: any;
public parentId?: number;
public name?: string;
public children: Children[]
constructor(data: IChildren) {
Object.assign(this, data);
}
}
Please help me I am new in angular and typescript. Thank you