如何在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
<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分配数组的索引
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);