簡體   English   中英

如何檢查數組並在JavaScript中添加新的Cookie

[英]How to check array and add new cookie in javascript

我想將元素與isnt cookie進行比較。 為此,我制作了腳本(僅供控制台使用)-它正在工作,但僅檢查一個-第一個cookie,然后添加第二個。 例如,當我有2個cookie-test1,test2時,它不起作用。 我可以用什么方式更改它?

var arrMy = ["test1", "test2", "test3", "test4", "test5"];
var i = 0;
function nextItem() {
    i = i + 1; /
    i = i % arrMy.length;
    return arrMy[i];
}
var x = nextItem();
console.log(x);

for(document.cookie.indexOf(x) > -1 == true;;){
function nextItem2() {
    i = i + 1;
    i = i % arrMy.length; 
    return arrMy[i];
}

var w = nextItem2();
if(x != w){
break;
}
}
document.cookie =  w+'=yet another test; expires=Fri, 27 Jul 2021 02:47:11 UTC; path=/'
//get the cookies:
var cookies=document.cookie.split(";");
//cookies to add
var add=["test=1","test=2"]
//loop trough the cookies to add
add.forEach(cookie=>{
       //if the cookie isnt found
       if(!cookies.find(el=>el.split("=")[0]===cookie.split("=")[0])){
            //add the cookie
           cookies.push(cookie);       
        }
});
document.cookie=cookies.join(";");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM