返回字符串而不是null

时间:2016-01-05 08:53:48

标签: javascript jquery string null

我正在使用API​​来获取要附加到DOM的值,在数组中,一些值返回null,在表中将它们留空。我的问题是可以返回一个字符串,表明“信息丢失”而不是返回空?

success: function(currency) {
    // loop through currency
    for (var i = 0; i < currency.length; i++) {
        if (currency[i].currency == userCurrency) {
            var $tr = $("<tr />");
            $tr.append($("<td />").text(currency[i].volume));
            $tr.append($("<td />").text(currency[i].latest_trade));
            $tr.append($("<td />").text(currency[i].bid));
            $tr.append($("<td />").text(currency[i].high));
            $("#theTable tbody").append($tr);
        }
    }
}
});
});
});

1 个答案:

答案 0 :(得分:5)

像这样使用|| "information is missing": -

$tr.append( $("<td />").text(currency[i].volume || "information is missing"));

然后,这将使用"information is missing"表示任何错误值,即:0, null, undefined, "", false

虽然如果您只想查看null,可以使用内联: -

$tr.append( $("<td />").text(currency[i].volume != null ? currency[i].volume : "information is missing"));
相关问题