繁体   English   中英

有什么办法可以避免 localStorage.getItem() 的 key 的未定义值?

[英]Is there any way to avoid the undefined value of key of localStorage.getItem()?

我的本地存储中有以下密钥对值 - 仪器,BTX(键,值)。 我从本地存储中手动删除了该值。 现在这个键的值是未定义的。 我写了这个条件来避免未定义的值,但它不起作用。 这里的问题是,虽然该值是未定义的,所以应该执行 else 块,但在所有可能的情况下,if 块都会被执行。

第一种方式:

if(localStorage.getItem("instrument")){}
else {}

第二种方式:

if(!!localStorage.getItem("instrument")){}
else {}

第三种方式:

if(localStorage.getItem("instrument")!==undefined){}
else {}

尝试这个

if(localStorage.getItem("instrument")!=null){
// Do Something here
}else{
// Do Something Here
}

希望它的工作:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM