简体   繁体   English

如何检查数组并在JavaScript中添加新的Cookie

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

I want to compare elements with isnt cookies. 我想将元素与isnt cookie进行比较。 For that I made script (only for use by console) - it is working but is checking only one - first cookie and add second. 为此,我制作了脚本(仅供控制台使用)-它正在工作,但仅检查一个-第一个cookie,然后添加第二个。 When I have for example 2 cookies - test1, test2 it's not working. 例如,当我有2个cookie-test1,test2时,它不起作用。 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(";");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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