How to ignore properties sent via http

I have an interface in my application that supports properties that I want to send to my database, as well as properties that I do not have.

In particular, I support a property called state , which can be set to open or null (closed), which then calls Angular2 state animation. I use this on *ngFor lists to open the closure of the item information panel.

However, I do not want to store the state value in my database, since it is always null by default. I am currently passing the entire object to an HTTP call, so the state property is also being sent. How can I ignore it?

  pushItemToDay(item: any, dateStr: Date): void { let body = JSON.stringify(item); let headers = new Headers({ 'Content-Type': 'application/json' }); let options = new RequestOptions({ headers: headers }); this.http.post(this.baseURL + 'api/addItem/' + dateStr, body, options) .toPromise() .catch(this.handleError); } 
0
source share
1 answer

Delete can be harmful if the object is used after the message. The stringify function has an additional parameter to ignore unwanted entries.

 let source = { 'meal': 'burger', 'milkshake': 'chocolat', 'extra':'2 hot dogs', 'free': 'smile' }; let ignoreList = [ 'meal', 'extra' ]; function replacer(key,value) { if (ignoreList.indexOf(key) > -1) return undefined; else return value; } let data = JSON.stringify(source, replacer); console.log(data); 
+4
source

Source: https://habr.com/ru/post/1261135/


All Articles