简体   繁体   English

如何将变量 = 设置为本地存储中的键

[英]How to set up a variable = to a key in local storage

I try to create a variable with the key of my local storage as it value.我尝试使用本地存储的键创建一个变量作为它的值。

i tried let justId = localStorage.key我试过let justId = localStorage.key

but the response i got in the console is just ƒ key() { [native code] } and i'm not able to use it on my code但是我在控制台中得到的响应只是ƒ key() { [native code] }而我无法在我的代码中使用它

Could you help me with that ?你能帮我解决这个问题吗? Thanks in advance提前致谢

To get and item or value from the localStorage , first you need to set a value.要从localStorage获取项目或值,首先需要设置一个值。

localStorage.setItem('key', 'value');

// To get an item:
var result = localStorage.getItem('key');

The localStorage.key is used to get the key using the value localStorage.key用于使用value获取key

localStorage.setItem('name', 'My Name')

var myKey = localStorage.key('MyName')
console.log(myKey)
// logs 'name'

在此处输入图片说明

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

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