如何删除本地存储中的数据
How to To Delete The data in Localstorage
我需要什么
- 我需要在点击时切换图像。
- 就像如果用户 select 最喜欢的选择 & 然后它标记不喜欢的图像被更改并且数据从 localstoarge 中删除。
html代码
<div style="display:block; float:right; width:auto; color:#7c7c7c;">
<a href="javascript:void(0);" class="favourate_dextop" id="fav'.$data[$k]['id'].'" onClick=" favaorite('.$data[$k]['id'].',\''.$name_event.'\',\''.$event_city.'\',\''.$event_country.'\',\''.$event_urls.'\',this)"></a>
</div>
js代码
function favaorite(sess_id,name,city,country,event_url,pointer){
var eventData;
//is anything in localstorage?
if (localStorage.getItem('eventData') === null) {
eventData = [];
} else {
// Parse the serialized data back into an array of objects
eventData = JSON.parse(localStorage.getItem('eventData'));
//alert(eventData);
$.each(eventData, function(key, value){
//console.log(value);
var imageUrl='http://im.gifbt.com/images/star1_phonehover.png';
//var imageUrl='http://im.gifbt.com/images/star1_phone.png';
$(pointer).closest('.evt_date').find('.favourate_dextop').css('background-image', 'url("' + imageUrl + '")');
//$(pointer).closest('.evt_date').find('.favourate_dextop').css('background-image', 'url("' + imageUrl + '")');
});
}
var details={};
details.sess_id=sess_id;
details.name=name;
details.city=city;
details.country=country;
details.event_url=event_url;
// Push the new data (whether it be an object or anything else) onto the array
eventData.push(details);
// Alert the array value
//alert(eventData); // Should be something like [Object array]
// Re-serialize the array back into a string and store it in localStorage
var jsondata=localStorage.setItem('eventData', JSON.stringify(eventData));
}
问题
- 我是本地存储的新手,我需要知道如何从 json 字符串中删除数据。
- 我已经实现了添加到收藏夹,现在我需要标记为不喜欢。
数据存储:
[{
"sess_id":182104,
"name":"AUTOMECH FORMULA",
"city":"Cairo",
"country":"Egypt",
"event_url":"automech-formula"
},]
要从本地存储中删除数据,请使用 localStorage.removeItem('itemNam')
示例
localStorage.setItem('name','hello');
从您使用的 localStorage 中删除名称项
localStorage.removeItem('name');
但如果您的问题是如何从 JSON 对象中删除数据,那么您有两种方法
1:改变原来的json对象
删除originalJson.attributeName
originalJson = {name:'myname',age:30};//our object to test with
示例:
delete originalJson.age //in this case originalJson.age is no more available
2:不要更改原始对象,而是制作另一个副本
originalJson2 = JSON.stringify(originalJson);
originalJson2 = JSON.parse(originalJson2);
delete originalJson2.age //originalJson.age is available but originalJson2.age is not available
这里是:jsfiddle
假设 sess_id 是您的事件的唯一 ID
function unfavorite(sess_id){
var eventData = localStorage.getItem('eventData');
//is anything in localstorage?
if (localStorage.getItem('eventData') === null) {
console.log('invalid event');
}
eventData = JSON.parse(eventData);
// Keep all without the one with specified sess_id
$.grep(eventData, function(value) {
return value.sess_id != sess_id;
});
var jsondata=localStorage.setItem('eventData', JSON.stringify(eventData));
}
我需要什么
- 我需要在点击时切换图像。
- 就像如果用户 select 最喜欢的选择 & 然后它标记不喜欢的图像被更改并且数据从 localstoarge 中删除。
html代码
<div style="display:block; float:right; width:auto; color:#7c7c7c;">
<a href="javascript:void(0);" class="favourate_dextop" id="fav'.$data[$k]['id'].'" onClick=" favaorite('.$data[$k]['id'].',\''.$name_event.'\',\''.$event_city.'\',\''.$event_country.'\',\''.$event_urls.'\',this)"></a>
</div>
js代码
function favaorite(sess_id,name,city,country,event_url,pointer){
var eventData;
//is anything in localstorage?
if (localStorage.getItem('eventData') === null) {
eventData = [];
} else {
// Parse the serialized data back into an array of objects
eventData = JSON.parse(localStorage.getItem('eventData'));
//alert(eventData);
$.each(eventData, function(key, value){
//console.log(value);
var imageUrl='http://im.gifbt.com/images/star1_phonehover.png';
//var imageUrl='http://im.gifbt.com/images/star1_phone.png';
$(pointer).closest('.evt_date').find('.favourate_dextop').css('background-image', 'url("' + imageUrl + '")');
//$(pointer).closest('.evt_date').find('.favourate_dextop').css('background-image', 'url("' + imageUrl + '")');
});
}
var details={};
details.sess_id=sess_id;
details.name=name;
details.city=city;
details.country=country;
details.event_url=event_url;
// Push the new data (whether it be an object or anything else) onto the array
eventData.push(details);
// Alert the array value
//alert(eventData); // Should be something like [Object array]
// Re-serialize the array back into a string and store it in localStorage
var jsondata=localStorage.setItem('eventData', JSON.stringify(eventData));
}
问题
- 我是本地存储的新手,我需要知道如何从 json 字符串中删除数据。
- 我已经实现了添加到收藏夹,现在我需要标记为不喜欢。
数据存储:
[{
"sess_id":182104,
"name":"AUTOMECH FORMULA",
"city":"Cairo",
"country":"Egypt",
"event_url":"automech-formula"
},]
要从本地存储中删除数据,请使用 localStorage.removeItem('itemNam')
示例
localStorage.setItem('name','hello');
从您使用的 localStorage 中删除名称项
localStorage.removeItem('name');
但如果您的问题是如何从 JSON 对象中删除数据,那么您有两种方法
1:改变原来的json对象
删除originalJson.attributeName
originalJson = {name:'myname',age:30};//our object to test with
示例:
delete originalJson.age //in this case originalJson.age is no more available
2:不要更改原始对象,而是制作另一个副本
originalJson2 = JSON.stringify(originalJson);
originalJson2 = JSON.parse(originalJson2);
delete originalJson2.age //originalJson.age is available but originalJson2.age is not available
这里是:jsfiddle
假设 sess_id 是您的事件的唯一 ID
function unfavorite(sess_id){
var eventData = localStorage.getItem('eventData');
//is anything in localstorage?
if (localStorage.getItem('eventData') === null) {
console.log('invalid event');
}
eventData = JSON.parse(eventData);
// Keep all without the one with specified sess_id
$.grep(eventData, function(value) {
return value.sess_id != sess_id;
});
var jsondata=localStorage.setItem('eventData', JSON.stringify(eventData));
}