gpt4 book ai didi

javascript - Web存储API session 存储: delete all keys like XYZ

转载 作者:行者123 更新时间:2023-12-01 03:11:39 24 4
gpt4 key购买 nike

给定以下 HTML session 数据:

hasSession                            1
user user_abc
CosttoServe599a8e8421a80 25
runningTotal 100
Maintenance599a8e8421a80 5
BusinessEnhancements599a8e8421a80 25
BusinessEnhancements599a8e8421a94 25
Maintenance599a8e8421a94 10
FrontierInvestments599a8e8421a94 5
CosttoServe599a8e8421a86 0
FrontierInvestments599a8e8421a86 5

我正在尝试删除包含以下内容的所有键:

599a8e8421a94

有什么方法可以在 session 存储上执行类似/通配符类型删除吗?

最佳答案

循环访问 sessionStorage 对象并检查某个键是否包含 599a8e8421a94,如果包含,则将其删除:

for (var key in sessionStorage) {
if (key.indexOf('599a8e8421a94') !== -1)
sessionStorage.removeItem(key);
}

实际应用的示例:

/**  Define dummy data... **/
var obj = {
'no-clean': 'i will not be cleaned.',
'testing599a8e8421a94': 25,
'another_test599a8e8421a94': 'e,ndleknfew',
'599a8e8421a94somemoretesting': '2knmsdkgsdg'
};

/** Loop through our dummy data and add it to sessionStorage. **/
for (var key in obj) {
sessionStorage.setItem(key, obj[key]);
}

/** This is the part you will need, loop through sessionStorage object. **/
for (var key in sessionStorage) {
/** Check if the key contains the search string... **/
if (key.indexOf('599a8e8421a94') !== -1)
sessionStorage.removeItem(key)
}

console.log(sessionStorage)

JSFiddle - 打开控制台查看 sessionStorage 对象中剩余的键/值。

阅读 Material

How to check whether a string contains a substring in JavaScript?

关于javascript - Web存储API session 存储: delete all keys like XYZ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45791870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com