1

Am having some const array values and i have the response value in subscribe and i want to check whether the const array value is availabe in response.i dont know how to use map and filter in subscribe. if the value is available then the checkval should return true other wise false.i can able to did using some function but here i needs to use rxjs operators

 Response:
{
    "testLoad": 1603367083993,
    "checkData": "test1"
    "type": "flag",
    "title": "TestFlag",
    "styleId": "test",
    "BodyContent": {
        "properties": "undefined"
    }

}
 

       const styleid: Array<string> = ['test1','test2','test3'];
        public checkVal(){
        this.idVal =this.data.getresponse().subscribe((response => console.log(response ));
       }

since am new to angular rxjs can you please guide me and help out from this issue

  • Please provide more information about the response. Also "i want to check whether the const array value is availabe in response" can be interpreted several ways, please clarify what exactly means "value is availble in response" – MoxxiManagarm Oct 22 '20 at 11:35
  • @MoxxiManagarm i have added the response – Front End Developer Oct 22 '20 at 11:53

1 Answers1

0

As per comments I understand, that you want to return a true Observable, if the response.checkData value is included in the pageUid array.

You can do the following:

  this.data.getresponse().pipe(
    map(response => pageUid.includes(response.checkData)),
  ).subscribe(checkResult => console.log(checkResult));
MoxxiManagarm
  • 8,735
  • 3
  • 14
  • 43
  • Hi is there any possible to instead of subscribe, modify this to observable and use async.if its possible means how can we do that – Front End Developer Oct 22 '20 at 12:41
  • Of course this is possible. Just remove the subscription and store the observable – MoxxiManagarm Oct 22 '20 at 12:49
  • removing subsribtion means could n't get you.can you please give some syntax since am new to angular and rxjs – Front End Developer Oct 22 '20 at 12:59
  • `checkResult$: Observable = this.data.getresponse().pipe(map(response => pageUid.includes(response.checkData)));` and in template use this observable with the async pipe `
    Valid: {{ checkResult$ | async }}
    `
    – MoxxiManagarm Oct 22 '20 at 13:06
  • if i remove subscribtion means it does not return true or false for eg: rxjs.for eg:this.data.getresponse().pipe( map(response => pageUid.includes(response.checkData)), ) if i remove the subscription means then it not returns true or false – Front End Developer Oct 22 '20 at 13:09
  • It returns an Observable with contains the boolean value. With the async pipe you observe this Observable – MoxxiManagarm Oct 22 '20 at 13:11
  • it returns object as obseravable but not as boolean – Front End Developer Oct 22 '20 at 13:29
  • like this Store {_isScalar: false, actionsObserver: ActionsSubject, reducerManager: ReducerManager, source: Store, operator: MapOperator} actionsObserver: ActionsSubject {_isScalar: false, observers: Array(1), closed: false, isStopped: false, hasError: false, …} operator: MapOperator {thisArg: undefined, project: ƒ} reducerManager: ReducerManager {_isScalar: false, observers: Array(1), closed: false, isStopped: false, hasError: false, …} source: Store {_isScalar: false, actionsObserver: ActionsSubject,. – Front End Developer Oct 22 '20 at 13:31
  • You can't turn that observable into a plain boolean, since you have an async operation here. Use either the async pipe or assign the boolean in the subscription callback – MoxxiManagarm Oct 22 '20 at 13:52