封装cookie 获取&设置方法
const getCook =(arr)=>{//
const cookie= document.cookie;
const res=arr.map(item=>{
const result =cookie.split("; ").map(val=>{
if(val.split("=")[0]===item) {//[name,zhang]
return val.split("=")[1]
}
}) //[name=zhang,age=18]
const result1=result.filter(val=>val)[0]
return result1
})
return res //[true]
}
const setCook =(obj)=>{
const keys =Object.keys(obj)//[name,age];
const values =Object.values(obj)//['zhang',18]
keys.forEach((item,index)=>{//cookie必须一次一次分别赋值
document.cookie=item+"="+values[index]
})
}
export {getCook,setCook}