1
votes

I'm currently using switchMap and combineLatest to change one of my properties from a reference to a string. I setup a custom pipe (refPipe) that takes a reference and returns an observable of that reference. The below method works just fine but I have another property (data.employees) that is an array of references. How can I iterate through that array and convert it into an array of employee objects in this case?

To clarify, I normally use my ref pipe in the html along with async but this does not work when it comes to filtering and sorting in a table so I'm mapping the data differently before I pass it to the table.

getClientEvents(): Observable<Event[]> {
    const eventsCollection = this.afs.collection<Event>('events');

    return eventsCollection.snapshotChanges().switchMap(events => {
      let arrayOfObservables = events.map(e => {
        const data = e.payload.doc.data() as Event;
        const id = e.payload.doc.id;
        return this.refPipe.transform(data.item).map(item => {
          data.item = item.name;

          data.employees // My array of references that I need to convert

          return { id, ...data };
        });
      });
      return Observable.combineLatest(arrayOfObservables);
    });
  }
1

1 Answers

0
votes

How can I iterate through that array and convert it into an array of employee objects in this case?

Well you have 2 options. You can either type assert:

someVar: Array<Employee> = data.employees as Array<Employee>

Or if you need to hit the Employee constructor:

someVar: Array<Employee> = data.employees.map(e => new Employee(e.a, e.b))