Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery:如果JSON数组中的元素为null,如何忽略它?_Javascript_Jquery_Json - Fatal编程技术网

Javascript jQuery:如果JSON数组中的元素为null,如何忽略它?

Javascript jQuery:如果JSON数组中的元素为null,如何忽略它?,javascript,jquery,json,Javascript,Jquery,Json,如果某个特定字段为null或空白,我希望忽略json数组的字段。我正在对当前抛出错误的json对象执行null验证。请告知我如何忽略json对象的空字段 var requestType = $("#requestType").val(); var requestorName = $("#requestorName").val(); var requestorDept =$("#requestorDept").val(); var requestorLocation =$("#requestorL

如果某个特定字段为null或空白,我希望忽略json数组的字段。我正在对当前抛出错误的json对象执行null验证。请告知我如何忽略json对象的空字段

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
}
我得到的错误如下

SyntaxError : missing ) after formal parameters
if(requestType !=undefined && issueType !=null && issueType !='')

您可以使用下面这样的实用方法

var validateNull=function(value){
        if(value!==undefined || value !== "null" || value !== "undefined" || value !== "" ){
            return true;
        }
        return false;
    }
在创建JSON时,请像这样使用它

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

…与所有其他字段类似

当前抛出错误,错误是什么?@ShaunakD我已更新错误详细信息。这可能会有所帮助。在obj文本中使用
if()..
的语法不正确。