Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何删除存储在本地存储器中的数据?_Javascript_Jquery_Arrays_Json_Local Storage - Fatal编程技术网

Javascript 如何删除存储在本地存储器中的数据?

Javascript 如何删除存储在本地存储器中的数据?,javascript,jquery,arrays,json,local-storage,Javascript,Jquery,Arrays,Json,Local Storage,如果我输入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"}]} {"dataCache":[{"id":53,"q

如果我输入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"}]}
{"dataCache":[{"id":53,"quantity":1,"total":200000,"request_date":"27-08-2017 20:38:00"}]}
我想按id删除缓存中的数据

例如,我删除id=20,它将删除缓存中的id=20

结果是这样的:

{"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"}]}
{"dataCache":[{"id":53,"quantity":1,"total":200000,"request_date":"27-08-2017 20:38:00"}]}

如何操作?

您需要检索对象并对其进行修改,然后将其再次存储在本地存储器中

var retrievedObj = JSON.parse(localStorage.getItem("cartCache"));
retrievedObj.dataCache[0].id = 53;
localStorage.setItem('cartCache', JSON.stringify(retrievedObj));

下面是一个示例解决方案

var data={“dataCache”:[{“id”:20,“数量”:1,“总计”:100000,“请求日期”:“27-08-2017 20:31:00”},{“id”:53,“数量”:1,“总计”:200000,“请求日期”:“27-08-2017 20:38:00”};
var-removeid=20;
var newData=$.grep(data.dataCache,函数(值){
返回值.id!=removeid;
});
console.log(newData)
您可以尝试以下方法:

const filtered = JSON.parse(localStorage.cartCache)
.dataCache.filter(filteredObj => filteredObj.id !== 20);

localStorage.cartCache = JSON.stringify({ "dataCache": filtered });
我在这里所做的只是使用
JSON
对象的
parse()
方法将
localStorage
中的项转换为JS对象。
我通过直接访问
dataCache
来链接调用,而不是将其分配给变量,然后我链接
filter()
调用,因为
dataCache
的返回值是一个数组。我正在筛选出
id
不等于
20
的数据
我将结果分配给
filtered
变量,然后调用
stringify()
传递
filtered
结果。
最后,我将其保存到
localStorage

//本地存储模拟
让localStorageMock=(函数(){
var存储={};
返回{
setItem:函数(键、值){
存储[键]=值| |“”;
},
getItem:函数(键){
返回存储器[键]| |空;
},
removeItem:功能(键){
删除存储器[键];
},
获取长度(){
返回对象。键(存储)。长度;
},
关键词:功能(一){
var keys=Object.keys(存储);
返回键[i]| | null;
}
};
})();
defineProperty(窗口'localStorage',{value:localStorageMock});
//你需要的代码
localStorage.cartCache=JSON.stringify({“dataCache”:[{“id”:20,“数量”:1,“总数”:100000,“请求日期”:“27-08-2017 20:31:00”},{“id”:53,“数量”:1,“总数”:200000,“请求日期”:“27-08-2017 20:38:00”});
const filtered=JSON.parse(localStorage.cartCache)
.dataCache.filter(filteredObj=>filteredObj.id!==20);
localStorage.cartCache=JSON.stringify({“dataCache”:filtered});
log(JSON.parse(localStorage.cartCache))您可以使用
数组#splice()
从数组中删除元素。使用
JSON.parse()
localStorage
获取对象,然后遍历数组,通过比较元素的id删除元素,然后将结果存储回localStorage

var str = localStorage.getItem('cartCache'); 
var cartCache = JSON.parse(str);
var idToRemove = 20;
cartCache.dataCache.forEach((obj,i) => {
  if(obj.id === idToRemove)
    cartCache.dataCache.splice(i,1);
});
localStorage.setItem('cartCache', JSON.stringify(cartCache));

对于localStorage,它是一个字符串,而不是一个对象。因此,您必须获取该项目,对其进行编辑并将其更新回来。