0

Normally there is a function localstorage.remove(key). But i want to delete an index within a key. I am saving the object like that:

cart[idstr] = [qty, name];

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

I want to delete a specific cart[idstr] Sample console.log(cart) is giving:

{pr22: Array(2), pr19: Array(2), pr21: Array(2)}

Suppose i want to delete pr19. What should i do?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • Does this answer your question? [How do I remove a property from a JavaScript object?](https://stackoverflow.com/questions/208105/how-do-i-remove-a-property-from-a-javascript-object) – Liam Oct 21 '20 at 07:17

6 Answers6

2

You need to deserialize, modify, and re-save the item.

Remember that localStorage is a simple key/value store for strings: it does not provide any way to manipulate the contents of those stored strings (you don't even need to store JSON, you could store YAML, XML, or Base64-encoded arbitrary binary data in localStorage.

Like so:

function storeCart( cart ) {
    
    localStorage.setItem( 'cart', JSON.stringify( cart ) );
}

/** Returns 'true' if the specified key/value was removed from the stored cart object - otherwise 'false'. */
function deleteItemFromStoredCart( key ) {
    
    const cartString = localStorage.getItem( 'cart' );
    if( !cartString ) return false;
    
    try {
        const cart = JSON.parse( cartString );
        if( !( key in cart ) ) return false;

        delete cart[ key ];
        storeCart( cart );
        return true;
    }
    catch( err ) {
        console.log( "Error: ", err );
        return false;
    }
}
Dai
  • 141,631
  • 28
  • 261
  • 374
1

I would suggest getting the data, deleting the object, then resetting it.

var cart = localStorage.getItem('cart');
cart = cart ? JSON.parse(cart) : {};
delete cart['pr19'];
localStorage.setItem('cart', JSON.stringify(cart));
Dharman
  • 30,962
  • 25
  • 85
  • 135
0

Try saving the cart object as a temp variable, removing cart from local storage, then removing the index from the temp and re-adding it to local storage. ie:

var temp = localStorage.getItem('cart')
localStorage.removeItem('cart')
delete temp[idstr]
localStorage.setItem('cart', temp)
  • This code will fail with a runtime `Error` if `temp` does not exist in `localStorage` - also the call to `localStorage.setItem` will fail because you're passing an `object`, not a `string`. – Dai Oct 21 '20 at 07:24
0

It is not possible to do it directly.

    • First you have to get the saved item from storage
    • Delete the property you want on this item
    • Update the item on storage
let cart= JSON.parse( localStorage.getItem('cart') );
delete cart.pr19;
localStorage.setItem('cart', JSON.stringify( cart ) );

0

for set value in localstorage : only one field directly write => localStorage.setItem('id',2); for object directly write => localStorage.setItem('object',JSON.stringify(object));

for get value from local storage : only one field directly write => localStorage.getItem('id'); for object directly write => JSON.parse(localStorage.getItem("object"))

for remove value in localstorage : => localStorage.removeItem('id');

-1

Try this

delete localStorage.cart[idstr]
Skywolfmo
  • 24
  • 6
  • This code won't work at all: `localStorage` is an object of type `Storage` which does not have implicit properties (and as I explained in my answer, `localStorage` only stores `string` values, not `object` values, so you must deserialize and reserialize objects). – Dai Oct 21 '20 at 08:13