1

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.

2
  • Object.keys(params).reduce((a,c) => params[c] ? {...a, [c]: params[c] } : a, {}) Commented Apr 12, 2020 at 8:46
  • I've updated my answer, I think you need some sort of map to link the params keys with the queryData keys as they are different. Please check the second snippet below and let me know if it solved your question. Commented Apr 12, 2020 at 9:07

2 Answers 2

1

You could just have a guard clause at the top of the function that will return from the function if params === null, like:

function(params) {
  if (params === null) return;

  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');
}

If you want to check if some value in params is falsy (null, undefined, NaN, empty string (""), false or 0), then you can do something like:

params = {
  exampleNo: 42,
  hospitalName: 'Gotham General Hospital',
  checkItem: null,
  reportDate: "01/01/1970",
};

let map = {
  exampleNo: "patient_id",
  hospitalName: "hospital_name",
  checkItem: "item_name",
  reportDate: "report_date",
};

let query = Object.entries(params).reduce((p, [k, v]) => {
  if (v) p[map[k]] = v;
  return p;
}, {});

console.log(query);

// {
//   "patient_id": 42,
//   "hospital_name": "Gotham General Hospital",
//   "report_date": "01/01/1970"
// }

Sign up to request clarification or add additional context in comments.

2 Comments

if the params contained value is null,how to handle property.
So you want to return in case any property of params is null or undefined? In that case you'll have to loop through them and check if any is null/undefined and return. The code above only works for the null case, if you want for undefined as well you can do if (!params) return;
1
let params = {
  exampleNo: null,
  hospitalName: undefined,
  checkItem: true,
  reportDate: "42",
};
let newObj = {};
Object.keys(params).forEach((item) =>{
    if(params[item]){ newObj[item] = params[item]}
} );
console.log('get newbj', newObj);

You can pass newObj as your queryData.

1 Comment

The queryData keys are different from the params keys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.