【问题标题】:How to create a multiple values for a single key using local storage如何使用本地存储为单个键创建多个值
【发布时间】:2014-08-24 01:19:59
【问题描述】:

众所周知,本地存储是一个键值对。尝试为单个键创建多个值。但无法了解如何为单个键传递多个值。

这里的创建很简单。

var value = "aa"
localStorage.setItem("testKey", value);
var test = localStorage.getItem("testKey");
alert(test);

现在这里想要实现的是testKey 应该有aa, bb and cc 值。

如果可以的话,任何人都可以帮我提供一个样本。

注意:

localStorage 值是否适用于本机应用程序。

【问题讨论】:

  • 您可以使用非冲突字符将保存的值拆分为数组。这很容易,因为 localStorage 是 unicode。与 JSON 的 3char-per-string 项目开销相比,以这种方式存储数千个短项目时节省了大量空间
  • @dandavis:Azmisov 在回答中给出的内容是一样的

标签: javascript jquery html local-storage


【解决方案1】:

您实际上可以做到这一点,例如使用 json 数组,您希望将此数组与两个项目一起存储

      var items =    {
            "1": {
            "id": 7606606,
            "item_id": "2",
            "row": {
                    "id": "2",
                    "amount": 0,
                    "plan": 1
                    }
                  },
             "2": {
            "id": 7606623,
            "item_id": "3",
            "row": {
                    "id": "3",
                    "amount": 0,
                    "plan": 1
                    }
                  }
           }

将其转换为 json,然后像这样存储在本地存储中

             localStorage.setItem('myItems', JSON.stringify(items));

然后你以同样的方式检索

             var items = JSON.parse(localStorage.getItem('myItems'));

然后你可以使用foreach来检索

【讨论】:

    【解决方案2】:
            <form>
        <label>Mortgage Amount</label>
        <input type="text" id="amount">
        <label>Interest Rate % </label>
        <input type="text" id="interest">
        <label>Mortgage Period (Years)</label>
        <input type="text" id="period">
        <input type="button" value="Submit" id="btn">
        <input type="button" value="GetLocalStorage" id="localbtn">
    </form>
               <script>
    $(document).ready(function () {
    
        $("#btn").click(function () {
    
            var principal = $("#amount").val();
            var interest = $("#interest").val();
            var years = $("#period").val();
            var item = [];
           item.push(principal,interest,years);
           localStorage.item += JSON.stringify({ "principalAmount": principal, "interestAmount": interest, "Period": years });
    
    
        });
    

    【讨论】:

    • 您可以使用单个键将多个值存储在本地存储中。只需在将值推送到数组后进行附加。 i-e.localStorage.item+=
    【解决方案3】:

    这对于本地存储是不可能的。但是,您可以将 JSON 字符串存储为键的值,并通过一些后处理,您可以提取三个变量:

    var value = ["aa","bb","cc"]
    localStorage.setItem("testKey", JSON.stringify(value));
    var test = JSON.parse(localStorage.getItem("testKey"));
    alert(test);
    

    【讨论】:

    • 非常感谢。对问题中已经提到的有疑问。原生应用是否也存储这个本地存储值
    • 据我所知,它会起作用。您只需要确保本机应用程序支持 JSON 方法。
    【解决方案4】:

    一个key在localStorage中只能有一个字符串值。您可以拥有多个具有不同名称的键,或者您可以对值进行一些编码。例如,您可以将所有值放入一个数组中,然后使用 JSON.stringify() 对其进行编码并将结果存储在 localStorage 中。当你读回数据时,你可以使用 JSON.parse() 把它变成一个数组。

    【讨论】:

    • 我不能在本地存储中存储数组或对象吗?
    猜你喜欢
    • 2018-04-16
    • 1970-01-01
    • 2023-03-11
    • 2014-03-29
    • 2011-11-26
    • 2012-09-03
    • 2018-04-30
    • 2021-03-09
    • 1970-01-01
    相关资源
    最近更新 更多