简体   繁体   中英

How Chain Map or Chain Filter inside of Angular Http request that returns a promise

I have some data that I am rejecting from a json array with lodash. I need to reject multiple key values that aren't desired.

I am thinking I can either chain map and use the reject function again or possibly write a predicate function for the reject method to use.

I think I may be overthinking the issue here, can someone help me wrap my head around this? Do I need to switch over to an observable for this to work?

I want to be able to reject BEER, LIQUOR, WINE, ETC. The data is nested as well.

Here is an example JSON object在此处输入图片说明

Here is my service.

getMenuByLocationID(locationID) {
return this._http.get('/api/locations/menu', {params: {locationID: locationID}})
.map((response: Response) =>
_.reject(response.json(), {
  _embedded: { 
    menu_categories: [ { 
      name: 'BEER'
    } ]
  }
}))
.toPromise()
.catch(this.handleError);

}

I don't think _.reject can handle multiple values in its matching criteria. So, you can use _.reject multiple time with each of your rejected values (BEER, WINE, ...) but it might not be the best solution.

Instead, you can use _.reject with a custom predicate function that test all your values at once. But you have to write this test, I don't think lodash offer a built in solution. But I can be wrong...

Something like that

gettMenuByLocationID (locationID) {
  return this._http.get('/api/locations/menu', {params: {locationID: locationID}})
             .map((response: Response) => {
               let data = _.reject(response.json(), {_embedded: {menu_categories: [{name: 'BEER'}]}})
               data     = _.reject(data, {_embedded: {menu_categories: [{name: 'WINE'}]}})
               return _.reject(data, {_embedded: {menu_categories: [{name: 'WHATEVER'}]}})
             })
             .toPromise()
             .catch(this.handleError)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM