gpt4 book ai didi

javascript - 返回字符串而不是 null

转载 作者:行者123 更新时间:2023-11-29 16:53:08 25 4
gpt4 key购买 nike

我正在使用 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);
}
}
}
});
});
});

最佳答案

使用|| “信息丢失” 像这样:-

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

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

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

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

关于javascript - 返回字符串而不是 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34607576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com