本文介绍了如何从sessionStorage(AngularJs)中删除单个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将列表添加到sessionStorage,例如:
I add list to sessionStorage like:
var item = new cartItem(product.id, product.name, product.price, qty);
orderItem.push(item);
sessionStorage.setItem('addedProductsList', JSON.stringify(orderItem));
var retrieveArray= JSON.parse(sessionStorage.addedProductsList);
及其工作正常,现在我想通过productId从此列表中删除单个对象。
and its working fine, now i want to remove a single object from this list by productId.
推荐答案
请参阅以下文章:
如果你想从会话存储中删除指定键/值对,你需要这样的smth:
If you want to remove specify key/value pair from session storage, you need smth like this:
sessionStorage.removeItem(key)
根据您的情况:
var retrieveArray= JSON.parse(sessionStorage.addedProductsList);
for (i=0; i<retrieveArray.length; i++){
if (retrieveArray[i].id == "Your ProductId") {
retrieveArray.splice(i,1);
}
}
sessionStorage.addedProductsList = retrieveArray;
此外,正确的实现允许您从sessionStorage中读取,写入和删除值,就好像它是常规对象。例如:
Additionally, proper implementations allow you to read, write, and remove values from sessionStorage as if it were a regular object. For example:
//save a value
sessionStorage.name = "Name";
//retrieve item
var name = sessionStorage.name;
//remove the key
delete sessionStorage.name;
这篇关于如何从sessionStorage(AngularJs)中删除单个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!