How to retrieve all localStorage items without knowing the keys in advance?

前端 未结 8 1474
天涯浪人
天涯浪人 2020-12-04 13:59

I want to show all of the keys and storage written before. My code is below. I created a function (allStorage) but it doesn\'t work. How can I do this?

           


        
8条回答
  •  無奈伤痛
    2020-12-04 14:34

    // iterate localStorage
    for (var i = 0; i < localStorage.length; i++) {
    
      // set iteration key name
      var key = localStorage.key(i);
    
      // use key name to retrieve the corresponding value
      var value = localStorage.getItem(key);
    
      // console.log the iteration key and value
      console.log('Key: ' + key + ', Value: ' + value);  
    
    }
    

提交回复
热议问题