I am add query string like this:
reportQuery: function (params) {
var that = this;
var queryData = {
"patient_id": params.exampleNo,
"hospital_name": params.hospitalName,
"item_name": params.checkItem,
"report_date": params.reportDate
};
API.executeGet(queryData, function (res) {
if (res.data.Data && res.data.Data.length > 0) {
that.setData({
reportList: res.data.Data,
containsData: true
});
}
}, '/api/Analysis/Search', 'post');
}
but sometimes the params.exampleNo or params.checkItem is null or undefined. What is the clean write way to add the value to query if it is not null? For example, if params.checkItem is null, my query should look like this:
var queryData = {
"patient_id": params.exampleNo,
"hospital_name": params.hospitalName,
"report_date": params.reportDate
};
when It is null, just ignore it. If write like this:
if(value){
var queryData ={
}
}
It is ugly.
Object.keys(params).reduce((a,c) => params[c] ? {...a, [c]: params[c] } : a, {})paramskeys with thequeryDatakeys as they are different. Please check the second snippet below and let me know if it solved your question.