Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中使用localStorage从数组中存储和获取数据_Javascript_Jquery_Arrays_Html - Fatal编程技术网

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

如何在javascript中使用localStorage从数组中存储和获取数据,javascript,jquery,arrays,html,Javascript,Jquery,Arrays,Html,我需要什么 我需要在数组中存储所有数据 然后获取数据 refrence link js代码 <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 sup

我需要什么

  • 我需要在数组中存储所有数据
  • 然后获取数据

    refrence link
    
js代码

   <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
           }
      }

问题

  • 我读了一篇关于检索密钥的文章
代码

if(本地存储)
{
if(localStorage.length)
{
for(var i=0;i
  • 我需要建议我应该使用json.parse检索数组或localstorage.length分配数组的索引

在使用阵列和本地存储时,基础知识相当简单

要存储,请使用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)
    }
}

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

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);

你能澄清一下你遇到的问题吗?那么问题是什么呢?检查一下。如果您有数组格式的数据,并且所有数组的长度都相等,它将自动将JSON串接/解析到localstorage。遵循以下步骤。1) 从本地存储中检索ID、cities等所有数据,并对其进行解析以返回阵列。2) 获取任意一个数组的长度并对其进行迭代。3) 也将当前计数用作其他数组的索引。很可能是因为OP希望存储多个事件,我希望在数组中存储多个事件,但在检查localstorage时,它会在localstoargei中显示最近单击的数据,该数据由id[]检查;var id=localStorage.getItem('id');console.log(id);id=JSON.parse(id);console.log(id.length)//未定义
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)
    }
}
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);