使用html5 localstorage可以找到某个键值对应的索引
window.localStorage.setItem("Georgia","Atlanta")
var x=window.localStorage.getItem("Georgia")
我在本地存储中存储了 50 个州和最大城市的列表。 使用上面的代码我可以轻松检索亚特兰大是“佐治亚州”最大的城市。 有没有一种简单的方法可以进行反向查找并搜索“Atlanta”并获得“Georgia”?
window.localStorage.setItem("Georgia","Atlanta")
var x=window.localStorage.getItem("Georgia")
I have a list of 50 states and the largest city stored in localstorage.
Using the code above I can easily retrieve that Atlanta is the largest city for "Georgia".
Is there an easy way to do a reverse lookup and search for "Atlanta" and get "Georgia"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
本地存储是从键到值的简单映射,所以不是。没有方法可以查找值的键,或者更准确地说,查找键,因为可能有多个。
您还可以存储城市->州的反向表来完成此操作:
因此,现在
setItem()
表示“X 和 Y 之间存在关系”,而getItem()
> 表示“X 和 Y 之间是否存在/什么关系?”理想情况下,您应该将它们放在两个不同的表中,以区分您正在谈论的关系类型(即州->城市和城市->州),但在这个简单的情况下应该没问题。
Local Storage is a simple map from key to value, so no. There is no method to look up the key for a value, or more accurately, the keys because there may be more than one.
You could additionally store a reverse table of city->state to accomplish this:
So now
setItem()
means "there is a relationship between X and Y" andgetItem()
means "is there a / what is the relationship between X and Y?"Ideally you'd have these in two different tables to separate out what type of relationship you're talking about (i.e., state->city and city->state) but you should be okay in this simple case.