【发布时间】:2017-04-19 10:32:29
【问题描述】:
您好,我有一个具有序列化参数值的 cookie。
i.e)criteria = "name=praveen&age=25&studying=false&working=true&something=value"
现在我必须在这个 cookie 字符串中更新 name = praveenkumar,age = 24,something = null。如果值为 null(something = null),则应将其从 cookie 中删除。以下是我正在使用的代码。
updateCookie({'name':'praveenkumar','age':24,'something':null})
var updateCookie = function(params) {
// split the cookie into array
var arr = $.cookie('criteria').split("&");
//loop through each element
for(var i = arr.length - 1; i >= 0; i--) {
// getting the key i.e) name,age
var key = arr[i].split("=")[0];
// if the key is in given param updating the value
if( key in params) {
// if vale null remove the element from array or update the value
if(params[key] !== null) {
arr[i] = key+"="+params[key];
}
else {
arr.splice(i, 1);
}
}
}
// join array by & to frame cookie string again
$.cookie('criteria' , arr.join("&"));
};
它正在工作。但是如果 cookie 的大小变大,我会担心性能。
【问题讨论】:
标签: javascript jquery arrays cookies serialization