返回字符串而不是null

Jul*_*ega 2 javascript string null jquery

我正在使用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);
        }
    }
}
});
});
});
Run Code Online (Sandbox Code Playgroud)

Ben*_*enG 5

使用|| "information is missing"如下: -

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

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

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

$tr.append( $("<td />").text(currency[i].volume != null ? currency[i].volume : "information is missing"));
Run Code Online (Sandbox Code Playgroud)