1

In my project i want to make a model contains nested data based on mutiple api calls.(i mean after calling api based on ids calling another api on click);

  1. First API call.
   [
       {
           id: 1,
           name: "user 1"
       },
       {
           id: 1,
           name: "user 2"
       }
   ]
  1. By using these parent ids i am calling another api for the childrens. for example id = 1
   [
       {
           id: 3,
           name: "child 1",
           parentId: 1
       },
       {
           id: 3,
           name: "child 2",
           parentId: 1
       }
   ]
  1. Again using these child ids calling another api for sub childs.
   [
       {
           id: 5,
           name: "baby 1",
           parentId: 3
       },
       {
           id: 5,
           name: "baby 2",
           parentId: 3
       }
   ]

and this is further.... calling..

so finally the final data like this

   [
       {
            id: 1,
            name: "user 1",
            children:    [
               {
                   id: 3,
                   name: "child 1",
                   parentId: 1,
                   children: [
                        {
                           id: 5,
                           name: "baby 1",
                           parentId: 3,
                           children: // further
                        },
                        {
                           id: 5,
                           name: "baby 2",
                           parentId: 3,
                           children: // further
                        }
                   ]
               },
               {
                   id: 3,
                   name: "child 2",
                   parentId: 1,
                   children: // same structure
               }
           ]
       },
       {
           id: 1,
           name: "user 2",
           // same structure
       }
   ]
export interface IUser {
  id?: any;
  name?: string; 
  children?: // here what i need to do
  parentId?: any;
  active: boolean;
}

export class User implements IUser {
  public id?: any;
  public name?: string;
  public children?: // here what i need to do
  public parentId?: any;
  public active = false;

  constructor(data: IUser) {
    Object.assign(this, data);
  }
}
2
  • What's the question? Commented Aug 24, 2020 at 8:36
  • @IngoBürk as in my question how to initialize children data in my model. Commented Aug 24, 2020 at 8:39

2 Answers 2

3

If think this is simple as :

export interface Model {
   id: number;
   name: string;
   children?: Model[];
   parentId?: number;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try something like this

interface PersonModel {
   id: number;
   name: string;
   children?: PersonModel[];
   parentId?: number;
}

const persons: PersonModel[] = [
  {
    id: 1,
    name: "user 1"
  },
  {
    id: 2,
    name: "user 2"
  },
{
    id: 3,
    name: "child 1",
    parentId: 1
  },
  {
    id: 4,
    name: "child 2",
    parentId: 1
  },
  {
    id: 5,
    name: "baby 1",
    parentId: 3
  },
  {
    id: 6,
    name: "baby 2",
    parentId: 3
  }
];

function getByParent(id?: number): Observable<PersonModel[]> {
  return of(persons.filter((person: PersonModel) => person.parentId === id));
}

function getByParentNested(id?: number): Observable<PersonModel[]> {
  return getByParent(id).pipe(
    concatAll(),
    switchMap((person: PersonModel) => forkJoin(of(person), getByParentNested(person.id))),
    map(([person, children]) => ({ ...person, children})),
    toArray(),
  );
}

getByParentNested(undefined).subscribe(console.log);

Stackblitz: https://stackblitz.com/edit/rxjs-tupsnf?file=index.ts

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.