Friday 6 August 2021

How can I delete a specific JSON array (consultas) embedded within another JSON array (f7Contacts) both saved in a single localSorage key?

I have a localStorage key (f7Contacts) stored as a JSON array, as it should be. I know how to delete the whole key using localStorage.removeItem("myKey");.

My array, as you can see is formed by a set of JSON arrays embeeded inside of it. There is the wider one named f7Contacts and inside of it there are some subarrays (to name them someway) named visitings and consultas. Now, what I'm trying to delete is not a single item from any of the JSON arrays nor the whole f7Contacts array but the JSON sub array named consultas.

I have tried so many ways but I could not get a working code.

This is what I've got until now:

My stringify array looks like this seen at the dev console:

f7Contacts: [{
"id": "95470e9e-ee5c-4467-9500-1fcbeae1b57c",
"hasPic": true,
"picId": 3,
"createdOn": "2021-08-01T17:31:32.230Z",
"firstName": "Fulano Menganejo Ciclanejo",
"company": "La Fábrica",
"job": "Barrendero",
"mobile": "+53555555",
"phone": "+5372222222",
"sms": "",
"email": "fulano@nauta.cu",
"madedate": "Invalid date",
"clinic": "2",
"gender": "M",
"birthday": "Invalid date",
"age": "47",
"visitings": [{
    "id": "1",
    "vdate": "19/07/-2006 12:00 AM",
    "pdcb": "Est officia dolorem ",
    "vdiagnose": "Laborum Ea consecte",
    "vrecom": "Mollit cillum sed pe",
    "nextv": "Invalid date" }],

"consultas": [{
    "id": "1",
    "ldate": "04/07/-1978 12:00 AM",
    "lmotive": "Soluta cumque aspern",
    "ldiagnose": "Quidem autem do ut o",
    "ttest": "Rerum sint atque il",
    "etest": "Officiis deserunt un",
    "mtest": "Nihil a dolore rem s",
    "nextv": "Invalid date" },

    {"id": "2",
    "ldate": "04/04/-1991 12:00 AM",
    "lmotive": "Eius in est enim no",
    "ldiagnose": "In aliqua Sunt dol",
    "ttest": "Sunt dolor eu sed N",
    "etest": "Quia aut reprehender",
    "mtest": "Unde libero autem an",
    "nextv": "Invalid date"
}],

"isFavorite":true
}]

This is my html:

<a class="link" id="delete"><div class="float-button">Delete Consults</div></a>
<a class="link" id="dntdelete"><div class="float-button">Cancel</div></a>

<div style="text-align:center;background:#fff;padding:20px 0px 20px 0px;"><b>SURE YOU WANT TO DELETE?</b></div>
<ul>
<li>
<a href="#" class="item-link item-content" onclick="clearConsults()">
<div><b>DELETE CONSULTS</b></div>
</a>
</li>
</ul>

And this is my Javascript:

var contacts = [{
"id": "95470e9e-ee5c-4467-9500-1fcbeae1b57c",
"hasPic": true,
"picId": 3,
"createdOn": "2021-08-01T17:31:32.230Z",
"firstName": "Fulano Menganejo Ciclanejo",
"company": "La Fábrica",
"job": "Barrendero",
"mobile": "+53555555",
"phone": "+5372222222",
"sms": "",
"email": "fulano@nauta.cu",
"madedate": "Invalid date",
"clinic": "2",
"gender": "M",
"birthday": "Invalid date",
"age": "47",
"visitings": [{
    "id": "1",
    "vdate": "19/07/-2006 12:00 AM",
    "pdcb": "Est officia dolorem ",
    "vdiagnose": "Laborum Ea consecte",
    "vrecom": "Mollit cillum sed pe",
    "nextv": "Invalid date" }],

"consultas": [{
    "id": "1",
    "ldate": "04/07/-1978 12:00 AM",
    "lmotive": "Soluta cumque aspern",
    "ldiagnose": "Quidem autem do ut o",
    "ttest": "Rerum sint atque il",
    "etest": "Officiis deserunt un",
    "mtest": "Nihil a dolore rem s",
    "nextv": "Invalid date" },

    {"id": "2",
    "ldate": "04/04/-1991 12:00 AM",
    "lmotive": "Eius in est enim no",
    "ldiagnose": "In aliqua Sunt dol",
    "ttest": "Sunt dolor eu sed N",
    "etest": "Quia aut reprehender",
    "mtest": "Unde libero autem an",
    "nextv": "Invalid date"
}],

"isFavorite":true
}]

localStorage.setItem("f7Contacts", JSON.stringify(contacts));

function removeItem(id) {
var index = -1;
var contacts = JSON.parse(localStorage.getItem("f7Contacts")) || {}; //fetch f7Contacts from storage
var items = contacts.f7Contacts || []; //get the contacts
for (var i = 0; i < items.length; i++) { //loop over the collection
if (items[i].id === id) { //see if ids match
items.splice(i, 1); //remove item from array
break; //exit loop
}
}
localStorage.setItem("f7Contacts", JSON.stringify(contacts)); //set item back into storage
}

console.log(localStorage.f7Contacts);
removeItem("01");
console.log(localStorage.f7Contacts);;

The specific sub array I need to delete is named consultas:

"consultas": [{ "id": "1", "ldate": "04/07/-1978 12:00 AM", "lmotive": "Soluta cumque aspern", "ldiagnose": "Quidem autem do ut o", "ttest": "Rerum sint atque il", "etest": "Officiis deserunt un", "mtest": "Nihil a dolore rem s", "nextv": "Invalid date" }, {"id": "2", "ldate": "04/04/-1991 12:00 AM", "lmotive": "Eius in est enim no", "ldiagnose": "In aliqua Sunt dol", "ttest": "Sunt dolor eu sed N", "etest": "Quia aut reprehender", "mtest": "Unde libero autem an", "nextv": "Invalid date" }]

I must let it clear that this localStorage key (f7Contacts) is really stored by another js within the app and if I have included the localStorage.setItem("f7Contacts", JSON.stringify(contacts)); at the beginning of my code above is just as an attempt to make it clear for you.



from How can I delete a specific JSON array (consultas) embedded within another JSON array (f7Contacts) both saved in a single localSorage key?

No comments:

Post a Comment