admin管理员组

文章数量:1026399

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 !

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 !

本文标签: javascriptHow to update localstorage object value from index and keyStack Overflow