开发者

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('|');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜