javascript - 如何按键对localStorage进行排序
问题描述
出于某种原因,localStorage 不会按照您向其中添加数据的顺序进行排序。
我已将 JS 日期毫秒存储为 localStorage 中的键。
如何从按 asc 键排序的 localStorage 中输出键和值?
我是否需要从 localStorage 创建另一个 VAR,然后对 VAR 进行排序?
解决方案
如何使用 JSON.stringify(yourObject) 将时间保存为单个对象
就像是
const jsTimes = localStorage.getItem("times");
const times = jsTimes ? JSON.parse(jsTimes) : {};
...
times[new Date().getTime()] = "Now is the time";
localStorage.setItem("times",JSON.stringify(times));
或者,如果您必须
const times = Object.keys(localStorage)
.filter(key => key.length === 13 && !isNaN(new Date(key))
.map(key => key)
.sort() // alphabetic sort is ok on a timestamp
.map(key => ({[key]: localStorage[key] }));
推荐阅读
- scala - sbt-assembly spark 不工作,但 sbt run 可以
- python - 当Apply方法返回多行时,Pandas Store DataFrame
- django - 使用 django-elasticsearch-dsl-drf 搜索多个索引并序列化多个文档类型
- python - DQN 在验证模式下表现不佳
- google-cloud-platform - 通过 gcloud CLI 安装 Cloud Build Local 后无法运行
- python - 如何将 ColorPicker 属性传递给根小部件
- r - 添加计数和百分比
- python-sphinx - 使用 reStructuredText (rst) 和 Sphinx,有没有办法列出所有引用(使用 ref、doc 等)某个页面的页面
- php-7 - 处理巨大的 XML 文件
- apache-kafka - 在 KafkaStreams 拓扑中重试消息