简体   繁体   English

如何从本地存储返回所有项目?

[英]How to return all items from local storage?

I have five items saved into my localStorage and each includes their own key and value.我有五个项目保存到我的 localStorage 中,每个项目都包含自己的键和值。 My end goal is to return an HTML list with each item included on it.我的最终目标是返回一个 HTML 列表,其中包含每个项目。 The current code does not return anything, if you have any idea or any other way let me know.当前代码不返回任何内容,如果您有任何想法或任何其他方式,请告诉我。

Thanks!谢谢!

showLocalStorage = () => {
      this.arrayLocalStorage = [];
      for(var i=0, len=localStorage.length; i<len; i++) {
        let key = localStorage.key(i)
        let value = localStorage[key]
        this.arrayLocalStorage.push(value)
      }
      this.arrayLocalStorage.map(item => {return item})
}

you can get all your localStorage keys by using .您可以使用 . Object.keys() Object.keys()

const allKeys = Object.keys(localstorage);

then you can return the list like this然后你可以像这样返回列表

return <ul> 
        { allKeys.map(key => <li> {localstorage.getItem(key)} </li>) }
    </ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM