-1

I want to ignore the fields to the json array if a particular field is null or blank. I am doing the null validation insdie the json object which is currently throws error. Kindly advice how do i ignore a null field to the json object.

var requestType = $("#requestType").val();
var requestorName = $("#requestorName").val();
var requestorDept =$("#requestorDept").val();
var requestorLocation =$("#requestorLocation").val();

var data = {
   if(requestType!=undefined && requestType!=null && requestType !='')
    {
       "RequestType" : requestType,
     }

   if(requestorName!=undefined && requestorName!=null && requestorName!='')
    {
        "RequestorName" : requestorName,
    }

  "RequestorDept" : requestorDept,
  "RequestorLocation" : requestorLocation
}

I am getting the error as below

SyntaxError : missing ) after formal parameters
if(requestType !=undefined && issueType !=null && issueType !='')
user6410893
  • 53
  • 3
  • 9

1 Answers1

0

You can keep a utility method like below

var validateNull=function(value){
        if(value!==undefined || value !== "null" || value !== "undefined" || value !== "" ){
            return true;
        }
        return false;
    }

While creating JSON use it like this

    var data = {};
if(validateNull(requestType )){
     data.requestType=requestType ; 
}

...Similar for all other fields

Vijendra Kumar Kulhade
  • 2,217
  • 3
  • 15
  • 25