【问题标题】:How can I remove data by index array which is stored in local storage?如何通过存储在本地存储中的索引数组删除数据?
【发布时间】:2017-08-28 14:55:01
【问题描述】:

如果我 console.log(localStorage.getItem("cartCache")),结果是这样的:

{"dataCache":[{"id":20,"quantity":1,"total":100000,"request_date":"27-08-2017 20:31:00"},{"id":53,"quantity":1,"total":200000,"request_date":"27-08-2017 20:38:00"}],"expired":"2017-08-28T03:55:21.521Z"}

我想通过索引数组删除缓存中的数据

例如,当我删除索引数组 = 0 时,结果是这样的:

{"dataCache":[{"id":53,"quantity":1,"total":200000,"request_date":"27-08-2017 20:38:00"}]}

另一个例子,当我删除索引数组 = 1 时,结果是这样的:

{"dataCache":[{"id":20,"quantity":1,"total":100000,"request_date":"27-08-2017 20:31:00"}]}

我该怎么做?

我尝试这样:

var deleteIndex = 1;
var retrievedObj = localStorage.getItem("cartCache")
delete retrievedObj['dataCache'][deleteIndex];

这似乎不是正确的方法

【问题讨论】:

标签: javascript jquery arrays json local-storage


【解决方案1】:

localStorage.getItem("cartCache") 返回两个对象 - dataCacheexpired。如果要删除dataCache的元素n,通过

删除
localStorage.getItem("cartCache").dataCache.splice(n,1);

要将其放入您的代码中:

var deleteIndex = 1;
var retrievedObj = localStorage.getItem("cartCache");
retrievedObj.dataCache.splice(deleteIndex,1);

然后再次console.log你的localStorage,你会看到元素被删除了。

【讨论】:

    【解决方案2】:

    您可以使用splice 从数组中删除一个项目

    // Clearing all localStorage value. No need to use below line in your code
    localStorage.clear();
    // to be stored object
    var x = {
      "dataCache": [{
        "id": 20,
        "quantity": 1,
        "total": 100000,
        "request_date": "27-08-2017 20:31:00"
      }, {
        "id": 53,
        "quantity": 1,
        "total": 200000,
        "request_date": "27-08-2017 20:38:00"
      }],
      "expired": "2017-08-28T03:55:21.521Z"
    }
    // Initially storing it in local storage using JSON.stringify
    localStorage.setItem('storeData', JSON.stringify(x));
    // Once stored retrieving the value using JSON.parse
    var getItem = JSON.parse(localStorage.getItem('storeData'));
    // setting the dataCache with new array. The new array will be created as splice is used. splice is used to remove an item from array,
    //0 is the index of the array, while second parameter 1 is to represent how many item to be removed starting from 0 ndex
    getItem.dataCache = getItem.dataCache.splice(0, 1);
    console.log(getItem); // the modified object
    // after operation setting it to local storage
    localStorage.setItem('storeData', JSON.stringify(getItem))
    

    DEMO

    【讨论】:

      【解决方案3】:

      关键字“delete”仅在retrieveObj 中删除deleteIndex,它不会更新您的localStorage 值。为了更新 localStorage 使用:

      var deleteIndex = 1;
      var retrievedObj = localStorage.getItem("cartCache")
      delete retrievedObj['dataCache'][deleteIndex];
      localStorage.setItem("cartCache", retrieveObj)
      

      【讨论】:

        猜你喜欢
        • 2018-02-04
        • 1970-01-01
        • 2016-08-21
        • 2018-01-25
        • 1970-01-01
        • 1970-01-01
        • 2020-03-14
        • 2017-11-07
        • 2016-12-09
        相关资源
        最近更新 更多