var cookieStr = "BIDUPSID=63796B5A5E7163886D6263545060A5C5; PSTM=1617344237; BAIDUID=8B6581AB7EB8BEA73C3E4A3AF2F039AF:FG=1; BD_UPN=12314753; MCITY=-332%3A; BAIDUID_BFESS=12557415CFBFF67CFFFF0FAC3A95EB6E:FG=1; BD_HOME=1; H_PS_PSSID=34100_31253_34004_33676_33607_34135_34111_26350_22159; delPer=0; BD_CK_SAM=1; PSINO=2; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; Hm_lvt_aec699bb6442ba076c8981c6dc490771=1623923620,1624155783; Hm_lpvt_aec699bb6442ba076c8981c6dc490771=1624155783; COOKIE_SESSION=154566_0_4_0_18_48_0_4_0_4_0_12_154504_0_5_0_1624163060_0_1624163055%7C6%230_0_1624163055%7C1; sug=3; sugstore=0; ORIGIN=2; bdime=0; BA_HECTOR=0g25al0h0h8425a04p1gctlqs0q";
// 模拟reduce
Array.prototype.myReduce = function(func, initValue) {
var len = this.length,
nextValue = initValue;
for(var i = 0; i < len; i++) {
nextValue = func(nextValue, this[i], i, this);
}
return nextValue;
}
function parseCookie(str) {
var obj = {};
var arr = cookieStr.split('; '); //转成数组
arr.myReduce(function(prev, ele) {
var newArr = ele.split('=');
prev[newArr[0]] = newArr[1];
return prev;
}, obj);
return obj;
}
console.log(parseCookie(cookieStr));
console