Storing array content in session storage [duplicate]
Asked Answered
A

1

10

I have data being displayed in a table. I delete a row, I need to hide it until that deletion is also exposed to the backend (It is exposed only after a minute). There is also auto-refresh that happens every 25 seconds, which brings the stale data (only after a minute, updated data is available to the backend).

I decided to use sessionStorage to store the deleted objects and then whenever the stale data comes I compare and not show in the table.

But sessionStorage doesn't support array. So when user deletes one object, goes to some other page, comes back and deletes another object (sessionStorage variable is overwritten) and then refreshes, only the last object deleted is hidden, all other deleted objects are shown

I am not sure how to store the deleted objects in session storage.

Amphicoelous answered 1/7, 2018 at 12:15 Comment(1)
HTTP doesn't support objects either. Yet you can send objects to the backend. How? By serializing them to JSON. So, why wouldn't you do the same to store whatever you want to store in session storage?Hybris
B
22

If you store array of items its pretty easy. You can store an array using json stringify:

sessionStorage.setItem('deletedItems', JSON.stringify(array))

Then retrieve it like this:

JSON.parse(sessionStorage.getItem('deletedItems'))

Before storing next deleted item you can then retrieve previous items in storage, push to existing array the new item and store it back

Baerl answered 1/7, 2018 at 12:20 Comment(1)
Ya, that is how I ended up doing. Created a temp array, and pushed into it.Amphicoelous

© 2022 - 2024 — McMap. All rights reserved.