【问题标题】:Fetch data from localstorage and perform some string operation on it从本地存储中获取数据并对其执行一些字符串操作
【发布时间】:2013-10-08 09:30:43
【问题描述】:

我在 localStorage 中有几个条目。现在我要从 localStorage 中获取所有数据并对其执行 replace 操作。

例如

for(var i=0; i<window.localStorage.length; i++) {
var strKey = window.localStorage.key(i);
var sKey = strKey.replace("abc","")
console.log(sKey +"\n");

}

在这种情况下,我无法获得所需的结果。 “abc”没有被替换。

【问题讨论】:

    标签: javascript jquery html offline


    【解决方案1】:

    要在本地存储中设置新值...您需要调用 setter 来设置键的值。

    如果要修改密钥本身,则必须删除旧密钥并使用新密钥更新本地存储对象。

    for(var i=0; i<window.localStorage.length; i++) {
    var strKey = window.localStorage.key(i);
    var sKey = strKey.replace("abc","")
    localStorage.setItem(strKey, sKey);
    console.log(sKey +"\n");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多