简体   繁体   English

检查JSON对象是否为null的快捷方式

[英]Shortcut to check if JSON objects are null

I'm writing a code to parse a JSON file. 我正在编写代码以解析JSON文件。 I was wondering if there is a shortcut to check if any of the objects is null and return "No Information" to each of them so that I don't have to repeat '+(item.xxx || 'No Information')+' . 我想知道是否有捷径来检查任何对象是否为空并向每个对象返回“无信息”,这样我就不必重复'+(item.xxx || 'No Information')+' Is there such a shortcut? 有这样的捷径吗?

For example, I'm doing this to check if the object is null: 例如,我这样做是为了检查对象是否为空:

$(data.items).each(function (index, item) { 
    item_html +='<h3>'+item.title+'</h3><p>'+(item.description || 'No Information')+'<p>'+(item.date || 'No Information')+'<p>'+(item.source || 'No Information')+'<p>'+(item.month|| 'No Information')+';
}

Is there any better alternative? 还有更好的选择吗?

jQuery has a jQuery.isEmptyObject function. jQuery具有jQuery.isEmptyObject函数。

$(data.items).each(function (index, item) { 
    if (!jQuery.isEmptyObject(item)) {
        item_html +='<h3>'+item.title+'</h3<p>'+item.description+'<p>'+item.date+'<p>'+item.source+'<p>'+item.month+';
    }
}

It's still not the worst idea to check each property to see if it's undefined. 检查每个属性以查看是否未定义仍然不是最坏的主意。 Try taking the logic out of the html building, like var itemDate = item.date || "No Information"; 尝试从html构建中var itemDate = item.date || "No Information";逻辑,例如var itemDate = item.date || "No Information"; var itemDate = item.date || "No Information";

You also have some HTML and Javascript syntax errors. 您还会遇到一些HTML和Javascript语法错误。 Close your <p> tags, and close your string at the end of the line. 关闭您的<p>标记,并在该行的末尾关闭您的字符串。

write a simple function 写一个简单的函数

function s(item){
    if (item==null){
        return "No Inofrmation";
    }else{
        return item;
    }
 }

USAGE 用法

$(data.items).each(function (index, item) { 
item_html +='<h3>'+item.title+'</h3><p>'+s(item.description)+'<p>'+s(item.date)+'<p>'+s(item.source)+'<p>'+s(item.month)'</p>';
}

This could be a first version: 这可能是第一个版本:

var na = function(message, def){
  def || (def = 'No Information');
  return message || def;
};

$(data.items).each(function (index, item) { 
   item_html +='<h3>'+item.title+'</h3><p>'+
     na(item.description)+'<p>'+
     na(item.date)+'<p>'+
     na(item.source)+'<p>'+
     na(item.month)+'<p>'
}

Although it would be better if you do this logic in your data source, from where you get data.items ? 尽管在数据源中执行此逻辑会更好,但是从哪里获取data.items呢?

you can also use the map command to preprocess your array. 您还可以使用map命令对数组进行预处理。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/map

var fields = ['title', 'field1', 'flield2'];
items.map(function(item){
    for(var f in fields){
        fn = fields[f];
        if(item[fn] == undefined || item[fn] == null){
            item[fn] = 'No Information';
        }
    }
});

This is sort of a "hack" but you could print as is then replace all ocurrences of undefined: 这有点像“ hack”,但是您可以按原样打印,然后替换所有未定义的事件:

$(data.items).each(function (index, item) { 
    item_html +='<h3>'+item.title+'</h3><p>'+item.description+'<p>'+item.date+'<p>'+item.source+'<p>'+item.month;
}

item_html.split(">undefined<").join(">No Information<");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM