最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove array item from localstorage - Stack Overflow

programmeradmin4浏览0评论

I have an array i stored(parsed it as json) in localstorage. I got it back into an object, removed some items from it and i need to update the key with the new values in the local storage.

var counter = [0,1,2];
var count = counter[0];
var questions = localStorage.getItem('questions');///questions had been stored in local storage with another function
console.log(questions);
questions = $.parseJSON(questions);

if (questions != 0){
    $('.question').html(questions[count]['question']);
    var options = (questions[count]['options']);
    options.forEach(function (item) {
        $('.options').html(item);
    });
    var index = counter.indexOf(1);
    questions = questions.splice(index, 1);
    console.log(questions);
    localStorage.removeItem('questions);
    counter = counter.splice(index, 0);

Now, when i remove the question key from the local storage, the whole question array is deleted, however, i only need to delete the particular question array that was passed.

I have an array i stored(parsed it as json) in localstorage. I got it back into an object, removed some items from it and i need to update the key with the new values in the local storage.

var counter = [0,1,2];
var count = counter[0];
var questions = localStorage.getItem('questions');///questions had been stored in local storage with another function
console.log(questions);
questions = $.parseJSON(questions);

if (questions != 0){
    $('.question').html(questions[count]['question']);
    var options = (questions[count]['options']);
    options.forEach(function (item) {
        $('.options').html(item);
    });
    var index = counter.indexOf(1);
    questions = questions.splice(index, 1);
    console.log(questions);
    localStorage.removeItem('questions);
    counter = counter.splice(index, 0);

Now, when i remove the question key from the local storage, the whole question array is deleted, however, i only need to delete the particular question array that was passed.

Share Improve this question asked Aug 3, 2016 at 15:49 Fadil Olamyy WahabFadil Olamyy Wahab 6262 gold badges6 silver badges15 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 7

Instead of deleting the key, just set it again with the new questions array:

questions.splice(index, 1);
localStorage.setItem('questions', JSON.stringify(questions));

Don't use removeItem() that as the name says removes the whole item from localStorage. Just do another setItem() to overwrite the old data.

Also you do:

questions = questions.splice(index, 1);

That is going to remove element(s) from the array and return them. questions will then be the removed element(s) and not your modified array. By your question you just want the modified array. So just do the splice() without the assignment

So your end code would be:

questions.splice(index, 1);
localStorage.setItem('questions',JSON.stringify(questions));

I have prepared this for you, you can use it as very simple.,

Live example and demo: LocalStorage Arrays: Add, Remove, Update and Beyond...

localStorage.setItem('questions', JSON.stringify(questions));

You need to just reuse the localStorage you have before. It will automatically update the previous one stored. Use this immediately after the delete method.

localStorage.setItem('questions', JSON.stringify(questions));
发布评论

评论列表(0)

  1. 暂无评论