最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to update localstorage object value from index and key - Stack Overflow

matteradmin6PV0评论

I need this solution for my project. its very important for me. i want to update object value with key and index, from local storage FOR my cart application.

(Decrease button for product quantity in cart)

Exmp.

function decreaseQuantity(index) {
  var oldQty = localStorage.cart[index].quantity;
  var newQty = oldQty - 1;
  localStorage.setItem(cart[index].quantity, newQty);
}

I need this solution for my project. its very important for me. i want to update object value with key and index, from local storage FOR my cart application.

(Decrease button for product quantity in cart)

Exmp.

function decreaseQuantity(index) {
  var oldQty = localStorage.cart[index].quantity;
  var newQty = oldQty - 1;
  localStorage.setItem(cart[index].quantity, newQty);
}
Share Improve this question edited Mar 3, 2019 at 7:29 Jack Bashford 44.2k11 gold badges55 silver badges82 bronze badges asked Mar 3, 2019 at 7:23 wploverwplover 852 silver badges9 bronze badges 9
  • 1 You're not using localStorage properly. – Jeto Commented Mar 3, 2019 at 7:25
  • That's not how you get or set to localStorage. Please go through Window.localStorage. localStorage uses string key-value pairs. How are you setting the cart to localStorage? – adiga Commented Mar 3, 2019 at 7:25
  • @jeto what's the truth of it ? – wplover Commented Mar 3, 2019 at 7:29
  • 1 Look here: https://developer.mozilla/en-US/docs/Web/API/Window/localStorage. Pay special attention to the way values are retrieved (with .getItem) and also the note: The keys and the values are always strings. You'll need to stringify your array and parse it when you retrieve it. – Mark Commented Mar 3, 2019 at 7:29
  • I think he's got the doc's URL by now, guys :) – Jeto Commented Mar 3, 2019 at 7:30
 |  Show 4 more ments

2 Answers 2

Reset to default 4

You can't store plex object in localStorage, you can store data as string only.

If you want to store the cart object to locaStorage , you need to serialize it as string using JSON.stringify and store it like following.

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

Note: here 'cart' is the key.

To get back, change it and store back, you need to do like following.

 var data = window.localStorage.getItem('cart');
        if (data != null) {
            let cart= JSON.parse(data);
              cart[index].quantity = cart[index].quantity -1;
             window.localStorage.setItem('cart', JSON.stringify(cart));

        } 

its worked for me @PSK thanks for your efforts @adiga I guess I made a logic error. thank you too, for your interest !

Post a comment

comment list (0)

  1. No comments so far