Question

How I can remove a specific item from array stored in localstorage and update the page...

How I can remove a specific item from array stored in localstorage and update the page immediately!
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

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));

Add a comment
Know the answer?
Add Answer to:
How I can remove a specific item from array stored in localstorage and update the page...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Java) how can i remove a element of a array if it contains a specific word?...

    (Java) how can i remove a element of a array if it contains a specific word? —> ( ex: word= hi) , so any element that has “hi” in it will be removed. and then recreate the array with the remaining elements?

  • How do you write an UPDATE to remove specific words in SQL? Question: 1. Write SQL...

    How do you write an UPDATE to remove specific words in SQL? Question: 1. Write SQL to remove 'Airlines' from any carrier that has it. For example, if 'Delta Airlines' is in the table, your SQL should change that value to just 'Delta'. This must be a single query that addresses all records that have 'Airlines' in them, and should fix them all at the same time. It must be written with the assumption that other records containing 'Airlines' could...

  • 5. A three-heap with n elements can be stored in an array A, where A[O] contains...

    5. A three-heap with n elements can be stored in an array A, where A[O] contains the root of the tree. a) Draw the three-heap that results from inserting 5, 2, 8, 3, 6, 4, 9, 7, 1 in that order into an initially empty three-heap. You do not need to show the array representation of the heap. You are only required to show the final tree, although if you draw intermediate trees. b) Assuming that elements are placed in...

  • how to prepare system flowcharting segments for processing transaction stored on magnetic tape to update a...

    how to prepare system flowcharting segments for processing transaction stored on magnetic tape to update a master file stored on magnet tape

  • 3. A queue implementation is being done using a circular array. It has a front and...

    3. A queue implementation is being done using a circular array. It has a front and a rear index for the array. Given that the queue length is q.length, give the following tests: How do you test if the queue is empty? How do you test if the queue is full? Assuming the queue is not empty, how do you remove an item from the queue? (How do you update the values for front and/or rear?) Assuming the queue is...

  • How do remove an object from list and add to another with indexes (java) Ex: I...

    How do remove an object from list and add to another with indexes (java) Ex: I have two separate list classes, Dogs and WalkedDogs, so once a dog from the Dog list has been walked, I want to remove that dog from the Dog list and at the same time then add it to the WalkedDogs list. By doing so via ui, so when main runs, the user is shown a list of Dogs, and chooses a dog with index...

  • Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr,...

    Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...

  • How can I sort a string array? I have the array String [] words = {apple,...

    How can I sort a string array? I have the array String [] words = {apple, null, banana, orange}; which needs to be sorted to be "apple, banana, orange, null." How do I sort so the strings are all on the left, and null is on the right?

  • Array manipulation (a) Write Java code for a method exchange (int [] a, int i, int...

    Array manipulation (a) Write Java code for a method exchange (int [] a, int i, int j) that exchanges the values stored at indices i and j in the array a. You do not need to worry about cases where either i or j is an invalid index. Give the best estimate you can for its time complexity (b) In an ordered array of n items, how can we determine whether or not an item belongs to the list using...

  • Hello, Suppose a homogeneous array with 8 rows and 6 columns is stored in column major...

    Hello, Suppose a homogeneous array with 8 rows and 6 columns is stored in column major order starting at address 20 (base ten). If each entry in the array requires only one memory cell, what is the address of the entry in the third row and fourth column? What if each entry requires two memory cells? What is the meaning of address 20 base ten? how I can drwa the array? Thanks,

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT