var details = [{
"Name": "Bhavani",
"Phone No": ["123456", "123456"],
"Email Id": ["mamata@gmail.com", "mamata@yahoo.com"],
"Birthday": "21/16/22/33"
}, {
"Name": "Mamata",
"Phone No": ["123456", "123456"],
"Email Id": ["mamata@gmail.com", "mamata@yahoo.com"],
"Birthday": "21/16/22/33"
}];
function add() {
localStorage.setItem('contacts', JSON.stringify(details));
str = localStorage.getItem("contacts");
}
function removeItems(){
var s = localStorage.removeItem("contacts");
alert(s);
}
如何只删除数组中的一个元素而不删除整个本地存储?
答案 0 :(得分:0)
我在评论下的评论代码示例 - 从Mamata删除电子邮件ID:
var details = JSON.parse(localStorage.getItem('contacts'));
details.filter(function(e) {
return e.Name === "Mamata"
}).forEach(function(element) {
delete element["Email Id"];
});
localStorage.setItem('contacts', JSON.stringify(details));
编辑:如果您要删除整个记录而不是其中的字段,则应执行此操作:
var details = JSON.parse(localStorage.getItem('contacts'));
details = details.filter(function(e) {
return e.Name !== "Mamata"
});
localStorage.setItem('contacts', JSON.stringify(details));
答案 1 :(得分:-2)
localStorage.setItem("contacts",localStorage.getItem("contacts").split(',').splice(...));