user3844586
user3844586

Reputation: 151

Delete Array from Multidimensional array stored in local storage using javascript

This is my multidimentional array:

[
    {"leads":"Akhil","email":"[email protected]","phone":"9999-999-999","referance":"ByReferance","nameref":"Anand","preftime":"Afteroon"},
    {"leads":"Anand","email":"[email protected]","phone":"9998789333","referance":"email","nameref":"AAA","preftime":"Afteroon"}
]

I want to delete this array from the Multidimensional array, with OnClick event of button

{"leads":"Akhil","email":"[email protected]","phone":"9999-999-999","referance":"ByReferance","nameref":"Anand","preftime":"Afteroon"}

,How it is possible?

Upvotes: 0

Views: 294

Answers (1)

gurvinder372
gurvinder372

Reputation: 68393

suppose this is the value obtained from localstorage.getItem("contacts");

var value = JSON.parse(localstorage.getItem("contacts"));

for the sake of this example

var value = [{"leads":"Akhil","email":"[email protected]","phone":"9999-999-999","referance":"ByReferance","nameref":"Anand","preftime":"Afteroon"},{"leads":"Anand","email":"[email protected]","phone":"9998789333","referance":"email","nameref":"AAA","preftime":"Afteroon"}];

Assuming you want to delete an item based on the leads name, you need to loop and find out the required index

var index = -1;
for ( var counter = 0; counter < value.length; counter++)
{
   if ( value[ counter ].leads == "Akhil" )
   {
      index = counter;
      break;
   }
}

all you need to do is remove first item from the value array

value.splice(0,1); // index = 1

and set the value back to localstorage

localstorage.setItem("contacts", JSON.stringify(value));

Upvotes: 1

Related Questions