title: how to store and fetch data from array using localStorage in javascript
body: <p>What i need</p>
  
  <ul>
  <li>i need to store all data in  array.</li>
  <li><p>and then fetch data .</p>
  
  <pre><code>refrence link
  </code></pre>
  
  <ol>
  <li><p><a href="https://stackoverflow.com/questions/3357553/how-to-store-an-array-in-localstorage">How do I store an array in localStorage?</a>.</p></li>
  <li><p><a href="http://blog.teamtreehouse.com/storing-data-on-the-client-with-localstorage" rel="nofollow noreferrer">http://blog.teamtreehouse.com/storing-data-on-the-client-with-localstorage</a></p></li>
  </ol></li>
  </ul>
  
  <p>js code </p>
  
  <pre><code>   &lt;script&gt;
  
   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
             }
        }
  </code></pre>
  
  <p>}</p>
  
  <p>/* fetch the data using from localstorage */</p>
  
  <pre><code>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));
  
  
  
         }
  </code></pre>
  
  <p></p>
  
  <p>problem</p>
  
  <ul>
  <li>i have read an article to retrieving of keys.</li>
  </ul>
  
  <p>code</p>
  
  <pre><code>      if (localStorage)
        {
          if (localStorage.length)
          {
              for (var i = 0; i &lt; 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');
          }
        }
  </code></pre>
  
  <ul>
  <li>i need suggestion i should use  json.parse to retreive array or localstorage.length assign index of array.</li>
  </ul>