2

I am requesting an API, that gives me an array back. Now I want to split this array into its components, so that we can make use of e.g. the "take" function of the Observables.

I am using Angular 2 / RxJS.

My current working code is:

public getFiltered(groupName:string, start?:number, num?:number):Observable<AddressGroupInfo> {
    start = start || 0;
    num = num || 0;

    return new Observable((observer) => {
        this.apiClient.post('AddressGroup/GetFiltered', {
            GroupName: groupName,
            StartValue: start,
            ResultCount: num
        }).map((pagedResultOfAddressGroupInfo:PagedResultOfAddressGroupInfo) => {
            return pagedResultOfAddressGroupInfo.ItemList;
        }).subscribe(
            (itemList) => {
                for (let item of itemList) {
                    observer.next(item);
                }
                observer.complete();
            },
            (error) => observer.error(error)
        );
    });
}

Is there a way to get rid of the outter Observable and just use a method on the inner observable to split the array?

thanks & greets

Dominik

1 Answer 1

1

Supposing your API returns a promise which resolves into an array, you could use a simple concatMap. For instance (jsfiddle):

public getFiltered(groupName:string, start?:number, num?:number):Observable<AddressGroupInfo> {
start = start || 0;
num = num || 0;

return Rx.Observable.from(this.apiClient.post('AddressGroup/GetFiltered', {
        GroupName: groupName,
        StartValue: start,
        ResultCount: num
    })).concatMap(x => x)
  });
}
Sign up to request clarification or add additional context in comments.

1 Comment

perfekt. you're my hero ;)

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.