How refactor this code?

0 like 0 dislike
3 views
Use on the project such a function to remove fields filter.
clearAllControl() { if (this.tableParam.region_id) { delete this.tableParam.region_id; this.searchForm.get('regionName').patchValue(null); } if (this.tableParam.district_id) { delete this.tableParam.district_id; this.searchForm.get('districtName').patchValue(null); } if (this.tableParam.area_id) { delete this.tableParam.area_id; this.searchForm.get('areaName').patchValue(null); } if (this.tableParam.city_id) { delete this.tableParam.city_id; this.searchForm.get('cityName').patchValue(null); } if (this.tableParam.place_id) { delete this.tableParam.place_id; this.searchForm.get('cityName').patchValue(null); } if (this.tableParam.name) { delete this.tableParam.name; this.searchForm.get('streetName').patchValue(null); } this.generateAddressesTable(this.tableParam); }


I don't really like this feature, tell me how can it refactor?
by | 3 views

2 Answers

0 like 0 dislike
[ { del: 'region_id', search: 'regionName' }, { del: 'district_id', search: 'districtName' }, { del: 'area_id', search: 'areaName' }, { del: 'city_id', search: 'cityName' }, { del: 'place_id', search: 'cityName' }, { del: 'name', search: 'streetName' } ].forEach(n => { if (this.tableParam[n.del]) { delete this.tableParam[n.del]; this.searchForm.get(n.search).patchValue(null); } });
by
0 like 0 dislike
+++
const config = [ 'region', 'district', 'area', 'city', {id:'place_id', name:'cityName'}, {id:'name', name:'streetName'}, ]; clearAllControl() { config.forEach(x=>{ let id = x.id || (x + '_id'); let name = x.name || (x + 'Name'); if (this.tableParam[id]) { delete this.tableParam[id]; this.searchForm.get(name).patchValue(null); } }); this.generateAddressesTable(this.tableParam); }
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked Mar 22, 2019 by xdenser
110,608 questions
257,186 answers
0 comments
28,699 users