I was working on a method that can erase all of the array content in "Bottin". The data is stored on the LocalStorage of the computer in a key called "Data".
Here's my sample of Json :
{
"descriptions": [
{
"Fabricant": "Test",
"Produit": "Test",
"Prix": "11.11",
"Details": " asdfasd",
"Categorie": "Categorie_Baseball",
"Images": "Hockey_Article_01.jpg"
},{
"Fabricant": "Test",
"Produit": "Test",
"Prix": "11.11",
"Details": " asdfasd",
"Categorie": "Categorie_Baseball",
"Images": "Hockey_Article_01.jpg"
},{
"Fabricant": "Test",
"Produit": "Test",
"Prix": "11.11",
"Details": " asdfasd",
"Categorie": "Categorie_Baseball",
"Images": "Hockey_Article_01.jpg"
}
],
"Bottin": [
{
"Nom": "Andy",
"Prenom": "Matador",
"Fonction": "dtesasd",
"Courriel": "[email protected]",
"Telephone": "515-555-5555"
}, {
"Nom": "Andy",
"Prenom": "Matador",
"Fonction": "dtesasd",
"Courriel": "[email protected]",
"Telephone": "515-555-5555"
},{
"Nom": "Andy",
"Prenom": "Matador",
"Fonction": "dtesasd",
"Courriel": "[email protected]",
"Telephone": "515-555-5555"
}
],
"users": {
"admin": "Inf2005"
}
}
Here's my partial solution :
function delAllBottin() {
bd = localStorage.getItem('data');
var descJsonObjects = bd.Bottin;
bd.Bottin.splice(0, descJsonObjects.length);
}
Doesn't seem to work for now, i don't know what i'm missing here ...
-
I think after delete, you should save the updated data again in localStorage.Neeraj Verma– Neeraj Verma2015年12月17日 05:16:52 +00:00Commented Dec 17, 2015 at 5:16
-
What do you mean by doesn't work? It shows error or just not removing the items?Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ– Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ2015年12月17日 05:17:20 +00:00Commented Dec 17, 2015 at 5:17
-
@NeerajVerma : Good point ! Let me test this tomorrow morning. Will keep you posted. Guess i was too much tired.Cyberflow– Cyberflow2015年12月17日 05:25:54 +00:00Commented Dec 17, 2015 at 5:25
-
@TareqMahmood : No error, its just not removing nothing.Cyberflow– Cyberflow2015年12月17日 05:26:32 +00:00Commented Dec 17, 2015 at 5:26
-
Have you tried my answer ?Chintan Soni– Chintan Soni2015年12月18日 06:29:06 +00:00Commented Dec 18, 2015 at 6:29
3 Answers 3
First of all, store json object in string format like:
var data = {
"descriptions": [
{
"Fabricant": "Test",
"Produit": "Test",
"Prix": "11.11",
"Details": " asdfasd",
"Categorie": "Categorie_Baseball",
"Images": "Hockey_Article_01.jpg"
},{
"Fabricant": "Test",
"Produit": "Test",
"Prix": "11.11",
"Details": " asdfasd",
"Categorie": "Categorie_Baseball",
"Images": "Hockey_Article_01.jpg"
},{
"Fabricant": "Test",
"Produit": "Test",
"Prix": "11.11",
"Details": " asdfasd",
"Categorie": "Categorie_Baseball",
"Images": "Hockey_Article_01.jpg"
}
],
"Bottin": [
{
"Nom": "Andy",
"Prenom": "Matador",
"Fonction": "dtesasd",
"Courriel": "[email protected]",
"Telephone": "515-555-5555"
}, {
"Nom": "Andy",
"Prenom": "Matador",
"Fonction": "dtesasd",
"Courriel": "[email protected]",
"Telephone": "515-555-5555"
},{
"Nom": "Andy",
"Prenom": "Matador",
"Fonction": "dtesasd",
"Courriel": "[email protected]",
"Telephone": "515-555-5555"
}
],
"users": {
"admin": "Inf2005"
}
}
localStorage.setItem("data", JSON.stringify(data));
Now, to clear Bottin array from it:
var data = JSON.parse(localStorage.getItem("data"));
data.Bottin = []; // array cleared
Now, save it again:
localStorage.setItem("data", JSON.stringify(data));
1 Comment
You cannot get / set an object as value for localStorage / sessionStorage. You need to make it a JSON string before setting and parse the JSON after getting from storage.
When setting
var data = {.....}; //Build your data object
localStorage.setItem('data', JSON.stringify(data));
When getting (and editing)
var jsonData = localStorage.getItem('data');
var data = JSON.parse(jsonData);
//If you want to do any modification, do it now
//then set it back to the storage
localStorage.setItem('data', JSON.stringify(data));
Comments
1 Parse the string in localStorage to JSON
2 Remove the item you don't want (with slice() )
3 Make the JSON to string
4 Re-set it in the localStorage
var items = JSON.parse(localStorage.getItem("data")); // updated
for (var i =0; i< items.length; i++) {
var items = JSON.parse(items[i]);
items.splice(i, 1);
}
item = JSON.stringify(items);
localStorage.setItem("data", items);