javascript localStorage 和布尔“字符串”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30644250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:28:29  来源:igfitidea点击:

localStorage and boolean 'string'

javascripthtmlbooleanlocal-storage

提问by A. Wolff

Storing boolean value in localStorage, this value is converted to string. Now trying to converting back this value from localStorage to boolean, i need to use JSON.parse()method, the more handy !!doesn't work.

在 localStorage 中存储布尔值,该值被转换为字符串。现在尝试将此值从 localStorage 转换回布尔值,我需要使用JSON.parse()方法,更方便的方法!!不起作用。

Code sample:

代码示例:

var test = false;
localStorage['test'] = test;
console.log("JSON.parse returns: ", JSON.parse(localStorage['test']), "expected: ", test);
console.log("'!!' returns: ", !! localStorage['test'], "expected: ", test);

-jsFiddle-

-jsFiddle-

I'm quite confused why this behaviour. Any explaination?

我很困惑为什么这种行为。有什么解释吗?

PS: using getter/setter localStorage methods doesn't matter here, same result.

PS:使用 getter/setter localStorage 方法在这里无关紧要,结果相同。

回答by Tiesselune

Local storage stores strings , I'm afraid, whatever the input (if you feed it with an object, it will be converted automatically with its standard toString()method)... So you're doing !! teston a string, which is always true.

本地存储存储字符串,恐怕无论输入如何(如果你用一个对象提供它,它将用它的标准toString()方法自动转换)......所以你!! test在一个字符串上做,它总是true.

You should always use JSON.stringify()and JSON.parse()when dealing with what you store in DOM storage

你应该总是使用JSON.stringify()JSON.parse()用打交道时,你在存储DOM存储

回答by Satpal

UseJSON.stringify()when save the object. As you know it will convert JavaScript value to a JSON string so when using JSON.parse()its converted back properly.

JSON.stringify()保存对象时使用。如您所知,它会将 JavaScript 值转换为 JSON 字符串,因此在JSON.parse()正确使用其转换回来时。

localStorage['test'] = JSON.stringify(test);

DEMO

演示

回答by Andrey

This happens because any stored value in localstorage is a string. So you've perofming !!"false"and !!is always truefor non-empty string. To be able to store non-string values in localStorage you have to always use JSON.

发生这种情况是因为 localstorage 中存储的任何值都是一个字符串。所以你已经 perofming!!"false"并且!!总是true用于非空字符串。为了能够在 localStorage 中存储非字符串值,您必须始终使用 JSON。

回答by Mac

What you want to do is simple with localDataStorage, where you can transparently set/get any of the following "types": Array, Boolean, Date, Float, Integer, Null, Object or String.

您想要做的很简单localDataStorage,您可以在其中透明地设置/获取以下任何“类型”:Array、Boolean、Date、Float、Integer、Null、Object 或 String。

[DISCLAIMER] I am the author of the utility [/DISCLAIMER]

[免责声明] 我是该实用程序的作者 [/免责声明]

Examples:

例子:

localDataStorage.set( 'test', false );

localDataStorage.get( 'test' );   -->   false

All of the conversion work is done in the background for you: just set/get and go.

所有转换工作都在后台为您完成:只需设置/获取即可。