Javascript - remove part of cookie with a split('|') like array
I'm abit stuck.
I'm using jQuery.
I have a cookie which has the value such as :
1,something|2,somethingg|1,something
We are treating it as [0] as id and [1] as name.
I need to remove ONE where the id == '1' for example, this will leave the cookie like this:
1,something|2,somethingg
How do I go about this, it will probally be in a loop, but not sure how to remove one of them. I have this so far开发者_运维知识库:
function removeItem(id){
var cookieName = 'myCookie';
var cookie = $.cookie(cookieName);
if(cookie){
var cookie = cookie.split('|');
$(cookie).each(function(index){
var thisCookieData = this.split(',');
if(thisCookieData[0] == id ){
}
});
}
You can call $.grep
, like this:
cookie = $.grep(cookie, function(item, index){
var parts = item.split(',');
return parts[0] !== id;
}).join('|');
精彩评论