如何在javascript中使用localStorage存储和获取数组中的数据

时间:2015-03-18 13:22:10

标签: javascript jquery arrays html5

我需要什么

js code

   <script>

 function favorite(sess_id,city,country,event_url)
{



 // Save data to the current local store//
    if (typeof(localStorage) == 'undefined' ) {
alert('Your browser does not support HTML5 localStorage. Try    upgrading.');
      }

      else
     {
      try {

      localStorage.setItem('id' ,sess_id);
      }
      catch (e)
      {
           if (e == QUOTA_EXCEEDED_ERR)
           {
               alert('Quota exceeded!'); //data wasn't successfully saved due to quota exceed so throw an error
           }
      }
      try {

      localStorage.setItem('city',city);
      }
      catch (e)
      {
           if (e == QUOTA_EXCEEDED_ERR)
           {
               alert('Quota exceeded!'); //data wasn't successfully saved due to quota exceed so throw an error
           }
      }
      try {

      localStorage.setItem('country',country);
      }
      catch (e)
      {
           if (e == QUOTA_EXCEEDED_ERR)
           {
               alert('Quota exceeded!'); //data wasn't successfully saved due to quota exceed so throw an error
           }
      }
      try
      {

      localStorage.setItem('event_url',event_url);
      }
      catch (e)
      {
           if (e == QUOTA_EXCEEDED_ERR)
           {
               alert('Quota exceeded!'); //data wasn't successfully saved due to quota exceed so throw an error
           }
      }

}

/ *使用localstorage * /

获取数据
var id= [];
var city = [];
var country =[];
var event_url= [];
// Retrieve the object from storage
      var retrievedObject = localStorage.getItem('id');
  console.log('id: ', JSON.parse(retrievedObject));
   var city = localStorage.getItem('city');
    console.log('city: ', JSON.parse(city));
    var country = localStorage.getItem('country');
    console.log('country: ', JSON.parse(country));
    var event_url = localStorage.getItem('event_url');
    console.log('event_url: ', JSON.parse(event_url));



       }

问题

  • 我读过一篇检索密钥的文章。

      if (localStorage)
      {
        if (localStorage.length)
        {
            for (var i = 0; i < localStorage.length; i++)
            {
            console.log(localStorage.key(i));
            id[i] = localStorage.getItem('id');
            city[i] = localStorage.getItem('city');
            country[i] = localStorage.getItem('country');
            event_url[i] = localStorage.getItem('event_url');
            console.log(id[i]);
            console.log(city[i]);
            console.log(country[i]);
            console.log(event_url[i]);

            }
        }
        else
        {
         alert('You have no favorite names stored');
        }
      }
  • 我需要建议我应该使用json.parse来检索数组或localstorage.length赋予数组索引。

2 个答案:

答案 0 :(得分:0)

使用数组和localStorage时,基础知识非常简单。

要存储,请使用JSON.stringify()将您的javascript数组转换为JSON,然后只需使用一个localSTorage键来设置值。

当您需要访问数组时,从localStorage中提取它并使用JSON.parse()将整个字符串转换回javascript数组。提取数据后,像使用任何其他数组一样使用数组,然后在完成操作后将其存储回相同的localStorage键

var existingArray=[
      {id:1 , country:'Country1', url:'example.com'}, 
      {id:2 , country:'Country2', url:'example2.com'}
];

localStorage.setItem('myEventsArray', JSON.stringify( existingArray));

/* on pageLoad */
var pageLoadArray = [];
var storedString = localStorage.getItem('myEventsArray');
if( storedString  ){
    pageLoadArray = JSON.parse(storedString);
    if( pageLoadArray.length){
       alert( pageLoadArray[0].country)
    }
}

答案 1 :(得分:0)

从本地存储中检索数据的方式似乎不对。我不明白,为什么要迭代本地存储长度。相反,你可以这样做。

var id, city, country,; event_url;  

// Retrieve the object from storage
var id = localStorage.getItem('id');
    id = JSON.parse(id);
var city = localStorage.getItem('city');
    city = JSON.parse(city);
var country = localStorage.getItem('country');
    country = JSON.parse(country);
var event_url = localStorage.getItem('event_url');
    event_url = JSON.parse(event_url);
相关问题