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);
}
How can I delete only one element from an array and not the whole local storage?
The code example for my comment under the question - removing Email Id from Mamata:
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));
EDIT : If you want to remove a whole record and not a field in it, this should do it:
var details = JSON.parse(localStorage.getItem('contacts'));
details = details.filter(function(e) {
return e.Name !== "Mamata"
});
localStorage.setItem('contacts', JSON.stringify(details));
localStorage.setItem("contacts",localStorage.getItem("contacts").split(',').splice(...));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.