如何查看Storage项是否设置?
如何检查 localStorage
中是否设置了某个项目?目前我正在使用
if (!(localStorage.getItem("infiniteScrollEnabled") == true || localStorage.getItem("infiniteScrollEnabled") == false)) {
// init variable/set default variable for item
localStorage.setItem("infiniteScrollEnabled", true);
}
How can I check if an item is set in localStorage
? Currently I am using
if (!(localStorage.getItem("infiniteScrollEnabled") == true || localStorage.getItem("infiniteScrollEnabled") == false)) {
// init variable/set default variable for item
localStorage.setItem("infiniteScrollEnabled", true);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
WebStorage 规范中的
getItem
方法,显式返回null
如果该项不存在:因此,您可以:
查看此相关问题:
The
getItem
method in the WebStorage specification, explicitly returnsnull
if the item does not exist:So, you can:
See this related question:
您可以使用
hasOwnProperty
方法来检查此功能在当前版本的 Chrome(Mac)、Firefox(Mac) 和 Safari 中是否有效。
You can use
hasOwnProperty
method to check thisWorks in current versions of Chrome(Mac), Firefox(Mac) and Safari.
如果密钥不在存储中,最短的方法是使用默认值:
The shortest way is to use default value, if key is not in storage:
有几种方法可以检查,我在这里添加它们
方法 1
方法 2
方法 3
方法 4
there are couple of methods to check i am adding them here
Method 1
Method 2
Method 3
Method 4
或
更新:
Or
Updated:
可以尝试这样的事情:
Can try something like this:
如果您想检查是否未定义,也可以尝试此操作:
getItem 是一种如果未找到值则返回 null 的方法。
You can also try this if you want to check for undefined:
getItem is a method which returns null if value is not found.
对于True
或 False
检查是否存在
For TRUE
FOR FALSE
CHECK EXISTANCE
如何测试
localStorage
中是否存在一项?此方法适用于 Internet Explorer。
How can one test existence of an item in
localStorage
?this method works in internet explorer.
我建议的最好、最安全的方法是这样,
它通过 ESLint 的
no-prototype -builtins
规则。Best and Safest way i can suggest is this,
This passes through ESLint's
no-prototype-builtins
rule.您应该检查 localStorage 中项目的类型
You should check for the type of the item in the localStorage
我已经在我的项目中使用过并且非常适合我
I've used in my project and works perfectly for me
我参加这个聚会迟到了,但是使用 localDataStorage,我创建的一个方便的实用程序包装器。
使用类似的方法实例化包装器后,
设置键
您可以以简单的方式 。请注意,此示例实际上将一个布尔值传递给存储,可以在存储中检索该值
,并且滚动状态将包含返回的布尔值。包装器无缝地为您跟踪本机 JavaScript 数据类型(数组、布尔值、日期、数字、对象等),代码中不再需要进行 JSON 字符串化/解析。
现在,当我们需要知道密钥是否在存储中时,我们可以像这样检查它。
您还可以检查特定值是否存在。例如
I'm late to this party, but checking localStorage for the existence of keys (or the existence of key values) is easily done with localDataStorage, a handy utility wrapper I created.
After instantiating the wrapper with something like
you can set keys
in a straightforward manner. Note that this example is actually passing a boolean value to the store, where it can be retrieved with
and scrollingState will contain the boolean value returned. The wrapper keeps track of the native JavaScript data type for you, seamlessly (Array, Boolean, Date, Number, Object, etc.) No more JSON stringifying/parsing in your code.
Now when we need to know if a key is in the store, we can check it like this
You can also check whether a particular value is present. For example
正如 @Christian C. Salvadó 上面提到的,你可以这样做
if (xxx === null){}
但 null 也是一个假值,如下所示:
它不会打印“hello”。
As @Christian C. Salvadó has mentioned above you can do
if (xxx === null){}
but null is also a falsy value, like so:
which does not print "hello".
也许最好扫描一下计划?
Maybe better to do a scan of the plan ?
我总是通过检查 localStorage 或 sessionStorage 的长度来检查它们是否已设置
I always check if localStorage or sessionStorage is set by checking the length of them
该代码
对我不起作用。也许是因为自从提供这个答案以来事情已经发生了变化。今天,2023 年,以下命令对我有用:
请注意,这实际上是 MDN Web 文档中推荐的方法,网址为 https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#testing_whether_your_storage_has_been_populated.
The code
is not working for me. Perhaps it's because things have changed since this answer was provided. Today, in 2023, the following command works for me:
Note that this is actually the method recommended in the MDN web docs at https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#testing_whether_your_storage_has_been_populated.
试试这个代码
Try this code