1

Currently I have datas variable that contains multiple element values.

like this

  ["WLP001","WLP002","WLP003","WLP004","WLP022"]

Deleting datas variable will be possible like this localStorage.removeItem("datas");

But if I have a variable in my js code like this var item = "WLP022";

and delete only the WLP022 inside of that datas would it be possible?

enter image description here

2
  • You have to get datas from localstorage then remove required values and set datas again. Commented Dec 11, 2020 at 4:38
  • yes, it's possible. you need to get the "datas" first and store it in a variable then that's the time you delete the value from array using .splice or by even filtering out the list. Then you just simply store it again to local storage with the new array value. Commented Dec 11, 2020 at 4:39

1 Answer 1

2

You can get index of item to delete then just use splice to remove that item from array and set your datas again in localStorage.

Demo Code :

var to_delete = "WLP003"
//var datas = localStorage.getItem('datas');//parse it
//suppose this is data
var datas = ["WLP001", "WLP002", "WLP003", "WLP004", "WLP022"];
var index = datas.indexOf(to_delete);//get index 
datas.splice(index, 1);//remove it
console.log(datas)
localStorage.setItem('datas', JSON.stringify(datas));//set again

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.