简体   繁体   中英

How to check array and add new cookie in javascript

I want to compare elements with isnt cookies. For that I made script (only for use by console) - it is working but is checking only one - first cookie and add second. When I have for example 2 cookies - test1, test2 it's not working. In what way I can change it?

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(";");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM