log(storedArr); // 输出: [1, 2, 3, 4, 5] 在这个示例中,JSON.stringify(arr) 将数组 arr 转换为 JSON 字符串,然后 localStorage.setItem('myArray', ...) 将这个字符串存储到 localStorage 中。读取时,localStorage.getItem('myArray') 获取到存储的 JSON 字符串,JSON.parse(...) 将这个字符串转换回数组。
localStorage.setItem('object',JSON.stringify({name:'对象'})); console.log(JSON.parse(localStorage.getItem('object')))//{name:'对象'} localStorage.setItem('array',JSON.stringify([1,2,3])); console.log(JSON.parse(localStorage.getItem('array')))//[1,2,3] localStorage.setItem('null',null...
getItem('arrayKey'); retrievedArray = JSON.parse(retrievedArray); 在这个例子中,我们首先使用JSON.stringify()方法将数组转换为字符串,然后使用localStorage.setItem()方法将字符串存储到localStorage中。要检索数组,我们使用localStorage.getItem()方法获取字符串,然后使用JSON.parse()方法将字符串转换回数组。 推荐的...
window.localStorage.setItem("user", JSON.stringify(data)); JSON.parse(window.localStorage.getItem("user")); 1. 2. 3. 4. 5. 6. Array let arr = ["h", "e", "l", "l", "o"]; window.localStorage.setItem("array", arr.join(",")); window.localStorage.getItem("array").split(","...
localStorage.setItem('a', Array(1024 * 1024 * 5).join('a')) localStorage.setItem('b', 'a') // Uncaught DOMException: Failed to execute 'setItem' on 'Storage': Setting the value of `a` exceeded the quota. 在上面的例子中,收到了一个错误,首先创建了一个5MB的大字符串,当再添加其他数据...
[1,2,3]//string[1, 2, 3]//array 2.存储对象(包括JSON对象)时如果不处理,得到的是对象元素的字符串, varobj = {"a": 1,"b": 2}; localStorage.setItem("temp2", obj); console.log(typeoflocalStorage.getItem("temp2")); console.log(localStorage.getItem("temp2"));//得到结果string ...
将更新后的数组重新存储到localStorage中。使用JSON.stringify()方法将数组转换为字符串,并使用localStorage.setItem()方法将其存储起来。 代码语言:txt 复制 localStorage.setItem('myArray', JSON.stringify(myArray)); 完整的代码示例: 代码语言:txt 复制
localStorage.setItem('name','value');只能整个array覆盖,如果你的value是一个array的话
1 2 3 4 5 varweekArray = ['周一'、'周二'、'周三'、'周四'、'周五']; //存: localStorage.setItem('weekDay',JSON.stringify(weekArray)); //取: weekArray = JSON.parse(localStorage.getItem('weekDay')); 标签:localStorage 好文要顶关注我收藏该文微信分享 ...
const store = localforage.createInstance({name: "nameHere"});const otherStore = localforage.createInstance({name: "otherName"});// 设置某个数据仓库 key 的值不会影响到另一个数据仓库store.setItem("key", "value");otherStore.setItem("key", "value2");复制代码 ...