gpt4 book ai didi

javascript - 如何检查响应变量是否存在且具有值

转载 作者:行者123 更新时间:2023-11-30 23:59:08 24 4
gpt4 key购买 nike

我有以下帖子,该帖子有效并获得了预期值。仅当 resp.count 的值为 1 或以上时,我才能进入 if block 。但我希望即使 resp.count 的值为 0 也能够进入 if block 。换句话说,我只是检查变量是否存在并具有值。我试图不使用类似下面的东西。

if(resp.count !== undefined){}

有没有更好的方法来更优雅地实现这一点?谢谢。

return post(api, { 'Content-Type': 'multipart/form-data' }, data)
.then((resp) => {
// count minimum value will be 0.
// Able to enter the following IF block only when value of count is 1 or above.
if (resp.count) {
// I want to get in here even if value is 0.
}
})

最佳答案

您可以安全地检查resp.count是否大于或等于零

if (resp.count >= 0) {

}

如果计数不存在,则不满足条件。

console.log(undefined >= 0)

关于javascript - 如何检查响应变量是否存在且具有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60852971/

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