开发者

parse JSON null values to get  ?

I'm parsing a JSON object (from YQL) that includes some null values, like this:

 {
     "color": "red",
     "size": null,
     "brand": "Nike",
 },
 {
     "color": null,
     "size": "XXL",
     "brand": "Reebok",
 },
 {
     "color": "blue",
     "size": "small",
     "brand": null,
 },

I'm using jQuery to generate some markup for it:

function (data) {
        $("#content").html('<table></table>');
        $.each(data.query.results.row, function (i, item) {
        $("table")
        .append('<tr><td class="color">' + item.color + '</td><td class="size">' + item.size + '</td><td class="brand">' + item.brand + '</td></tr>');
        });

What can I do (in jQuery ideally) to change the null's 开发者_开发知识库to a blank space, or at least put a class on their td? That is:

so that rather than getting

<td>null</td>

I get either

<td>&nbsp;</td>

or

<td class="hidethis">null</td>


You can use the shortcircuiting boolean OR operator, like this:

replace item.color with (item.color||"&nbsp;")


You can put an inline conditional. Its equivalent to

if( expr. ) {return true} else {return false}

<td class="brand">' + ( item.brand == null ) ? '&nbsp;' : item.brand + '</td></tr>'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜