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> </td>
or
<td class="hidethis">null</td>
You can use the shortcircuiting boolean OR operator, like this:
replace item.color
with (item.color||" ")
You can put an inline conditional. Its equivalent to
if( expr. ) {return true} else {return false}
<td class="brand">' + ( item.brand == null ) ? ' ' : item.brand + '</td></tr>'
精彩评论