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
2 Answers
Reset to default 4You 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
2 Answers
Reset to default 4You 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
版权声明:本文标题:javascript - How to update localstorage object value from index and key - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745641846a2160793.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论