如何删除以特定字符串开头的localStorage数据? [英] How to remove localStorage data starting with a certain string?

查看:166
本文介绍了如何删除以特定字符串开头的localStorage数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除与Firebase相关的localStorage.我试过了:

I want to remove Firebase-related localStorage. I tried this:

  let arr = [] // Array to hold the keys
  // Iterate over localStorage and insert the keys that meet the condition into arr
  for (let i = 0; i < localStorage.length; i++) { // eslint-disable-line
    console.log(localStorage.key(i)) // eslint-disable-line
    if (localStorage.key(i).substring(0,3) == 'firebase:') { // eslint-disable-line
      arr.push(localStorage.key(i)) // eslint-disable-line
    }
  }
  // Iterate over arr and remove the items by key
  for (let i = 0; i < arr.length; i++) {
    localStorage.removeItem(arr[i]) // eslint-disable-line
  }

但是它们根本没有被删除.

However they are not being removed at all.

我在做什么错了?

推荐答案

substring(0,3) == 'firebase:'

这不可能是真的.您将得到一个3个字符长的字符串,并将其与"firebase:"进行比较.只需将3更改为9,就可以这样:

This can never be true. You're getting a string 3 characters long and comparing it to "firebase:". Just change your 3 to a 9 so it looks like this:

substring(0, 9) == 'firebase:'

这篇关于如何删除以特定字符串开头的localStorage数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆