【问题标题】:How to update a specific item of an array in local storage如何更新本地存储中数组的特定项
【发布时间】:2017-05-17 23:54:16
【问题描述】:

我将这个值存储在我的键值中

现在在我的 HTML DOM 上,我添加了一些数据 [您可以使用任何变量给出示例],我想更新下面数组的 description: 参数,您能指导我如何简单地使用任何变量更新它的值变量值为例

key - Section283

价值 -

{
    "subtitle": "",
    "description": "<p>We are glad to hear from you!</p>",
    "id": "689",
    "title": "footer",
    "sectionType": "283",
    "latitude": "0",
    "longitude": "0",
    "tags": "",
    "userId": "32",
    "internalLink": "menu-lam"
}

【问题讨论】:

标签: javascript jquery html arrays


【解决方案1】:

首先,让我们明确一点,您根本没有数组。你展示的是一个对象。在纯 JavaScript 中,不需要引用键名,除非它们的名称中有空格。使用对象,更新其属性之一很容易:

// Note the the property names do not need to be quoted unless they contain spaces:
var myObject = {
        subtitle: "",
        description: "<p>We are glad to hear from you!</p>",
        id: "689",
        title: "footer",
        sectionType: "283",
        latitude: "0",
        longitude: "0",
        tags: "",
        userId: "32",
        internalLink: "menu-lam"
    }
    
    // Getting property value out of an object:
    console.log("Original value of \"description\" property: " + myObject.description);
    
    // Changing property value:
    myObject.description = "Something New";
    
    // Getting new value:
    console.log("Current value of \"description\" property: " + myObject.description);

当您需要在localStorage 中存储类似的内容时,必须将该对象转换为string(因为这是localStorage 唯一可以存储的数据类型)。您可以使用原生 JSON 对象轻松完成此操作,如下所示:

        var myObject = {
            subtitle: "",
            description: "<p>We are glad to hear from you!</p>",
            id: "689",
            title: "footer",
            sectionType: "283",
            latitude: "0",
            longitude: "0",
            tags: "",
            userId: "32",
            internalLink: "menu-lam"
        }
        
        console.log("Original type for \"myObject\": " + typeof myObject);
        
        // Convert object to JSON
        var jObj = JSON.stringify(myObject);
        
        // Check converted type:
        console.log("Type for \"myObject\": " + typeof jObj);

创建 JSON 字符串后,您可以将其存储在 localStorage 中,如下所示:

localStorage.setItem("myData", jObj);

然后,要检索数据,您可以编写:

var jsonString = localStorage.getItem("myData");

最后,需要将字符串转回对象,然后才能正常使用:

// This sets up what you'd get back from localStorage:
var localStorageString = `{
    "subtitle": "",
    "description": "<p>We are glad to hear from you!</p>",
    "id": "689",
    "title": "footer",
    "sectionType": "283",
    "latitude": "0",
    "longitude": "0",
    "tags": "",
    "userId": "32",
    "internalLink": "menu-lam"
}`;


// Convert the string back to an object
var obj = JSON.parse(localStorageString);

// Use the object as normal:
obj.description = "Something New";
console.log(obj.description);

【讨论】:

  • 感谢您这么好的回复,这让我清楚地了解了我想要做什么
【解决方案2】:

你可以使用这样的东西来设置localStorage

localStorage.setItem(key, 'Section283');

希望这会有所帮助!

【讨论】:

  • hii,设置本地存储后如何更新密钥名称?请指教
【解决方案3】:

这就是我的想法。使用您的密钥访问您的值,然后将其转换为 Javascript 对象并更新它的描述属性。然后将 json 对象转换回字符串,并使用带有 Array 的键来设置(基本上覆盖)该值。在这里暗中刺伤。

var jsonObj = json.parse(yourArray['Section283']);
jsonObj.description = 'Whatever you want here';
yourArray['Section283'] = JSON.stringify(jsonObj);

【讨论】:

    猜你喜欢
    • 2016-06-06
    • 2020-10-27
    • 1970-01-01
    • 2014-06-05
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多