How to read data from array after promise/observable call

442 Views Asked by At

I am not into javascript and angular, currently working on some UI task and has workable/little knowledge of javascript

what i want, base on a key field filter out the objects.

currently what code is doing, hitting a api, which is storing some result in an array. this array result are in object (promise object) i guess, and i need to set out a filter condition on this object base on some value.

but for me issue is, object does not have key same as response, it store. data like this

​Array(5) [ {…}, {…}, {…}, {…}, {…} ]
​
0: Object { _isScalar: false, source: {…}, operator: {…} }
​
1: Object { _isScalar: false, source: {…}, operator: {…} }
​
2: Object { _isScalar: false, source: {…}, operator: {…} }
​
3: Object { _isScalar: false, source: {…}, operator: {…} }
​
4: Object { _isScalar: false, source: {…}, operator: {…} }
​
length: 5

so even if i iterate through each object i cant find the key which is my filter criteria.

below is code related to this

getSomeThing(
        name: string,
        dcId: any
    ): Promise<any[]> {
        const url = `<url>`;
        return this.http.get(url, { headers: this.sharedService.getHeaders() })
            .toPromise()
            .then(response => this.getSomeThingOneByOne(name, dcId, response.json()))
            .catch(error => this.sharedService.handleError(error));
    }
private getSomeThingOneByOne(name, dcId, someIds): Promise<any> {
        const someObservables = [];
        someIds.forEach(some => someObservables.push(this.getsomethingObservable(name, dcid, some)));
 
        return Observable.forkJoin(someObservables)     
        .toPromise()
        .catch(error => this.sharedService.handleError(error));
    }
getsomethingObservable(
        name: string,
        dcId: any,
        someId: any
    ): Observable<any> {
        const url = `<url>`;
        return this.http.get(url, { headers: this.sharedService.getHeaders() })
            .map((response: Response) => {
                const vm = response.json();
                return vm;
            })
            .catch((error: any) => Observable.throw(this.sharedService.handleError(error)));
    }

note i have change the name of function and variable.,

now here, getsomeThing call an api which return a list of ids and pass it to getSomethingOneByOne as array ie [1,2,3,4,5] , and this function getSomethingOneByOne call getsomethingObservable to get the data related to that id.

what i want is once i recied the data i should check a certain value in it or in getsomethingObservable check the value it self for that key and filter out the result. but due since i am not able to read the data in array someObservables in function getSomeThingOneByOne raw i cant add filter condition there.

need help here as it requires knowledge of promise, obserable, angular and typescript which i lack.

1

There are 1 best solutions below

0
sahasrara62 On

just solved this by taking reference from @sonusindhu suggestion and taking reference from here too.

i had made changes on getSomeThingOneByOne as below and it worked

private getSomeThingOneByOne(name, dcId, someIds): Promise<any> {
        const someObservables = [];
        someIds.forEach(some => someObservables.push(this.getsomethingObservable(name, dcid, some)));
 
        return Observable.forkJoin(someObservables)     
        .toPromise()
        .then(response => {
             var data = response.filter(val=>val[<somekey>]!='<a certain value>');
          return data;
})
        .catch(error => this.sharedService.handleError(error));
    }