build object hierarchy using observables

Viewed 22

I am trying to build a object that represents a large community and all of its subcommunities. It essentially is a hiarchy of main community with multiple nested communities that have one or more nested communities themselves.

There is a factor of recursion to take into consideration, this is how the data structure should look like at the end:

{
  id: "1",
  name: "Car Club",
  communities: [
    {
      id: "11",
      name: "Euro Group",
      communities: [
        {
          id: "111",
          name: "Ferrari Club"
        }, {
          id: "112",
          name: "Audi Club"
        }

      ]
    },
    {
      id: "12",
      name: "Asian Fan Club",
      communities: [
        {
          id: "121",
          name: "Japanese Club",
          communities: [
            {
              id: "1211",
              name: "Toyota Group"
            }, {
              id: "1212",
              name: "Suzuki Group"
            }
          ]
        }, {
          id: "122",
          name: "Great Wall Group"
        }
      ]
    }
  ]
}

This is as far as i got before my brain blew up.

export class Community {
  id: string;
  name: string;
  communities: Community[]; // think of it sub communities
}

getCommunityById(id: string){
   this.communityService.getById(id) // this calls the remote server for data
   .subscribe((res: any) => {
        var community = new Community();
        community.id = res.id
        community.name = res.name;
        if (community.communities?.length) {
          community.communities = [];

          community.communities.forEach((e: any) => {
            var community = getCommunityById(e.id);
            community.communities.push(community);
          });
    }
  });
}

I know the above is incorrect, but here we are.

Appreciate helping me untangle the spaghetti code above.

1 Answers

So, if you only need an interface, you don't need to transform it into a class, the res already has the correct structure and you could call some static functions instead (where you pass the community interface as input).

but if you really really need a class, then this I think should work:

.subscribe((jsonCommunities: any) => {
    const newCommunity = new Community();
    newCommunity.id = jsonCommunities.id
    newCommunity.name = jsonCommunities.name;
    if (jsonCommunities.communities?.length) {
        newCommunity.communities = [];
        this.recursiveCommunity(jsonCommunities.communities, newCommunity);
    }
    console.log('Complete community tree: ', newCommunity)
});


recursiveCommunity(){jsonCommunities: any[], classCommunity:Community}{
    for(jsonCommunity of jsonCommunities){
        const newCommunity = new Community();
        newCommunity.id = jsonCommunity.id
        newCommunity.name = jsonCommunity.name;
        if (jsonCommunity.communities?.length) {
            newCommunity.communities = [];
            this.recursiveCommunity(jsonCommunity.communities, newCommunity);
        }
        classCommunity.communities.push(newCommunity);
    }

}
Related