逻辑运算符始终为真吗? [英] Logical Operator always stays true?

查看:120
本文介绍了逻辑运算符始终为真吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的是将当前URL与我的cookie数组进行比较,该cookie数组将包含用户访问过的所有URL,因此它将比较该数组是否包含当前链接,如果不包含,则将新链接推送到数组,并再次使用包含新推送链接的新数组重新创建cookie,所以我现在面对的是每次检查唯一链接的if函数总是实现时,我不确定是什么问题? / p>

请问人们:

 <脚本类型= text / javascript> 

函数createCookie(name,value,days){
var expires =;
if(days){
var date = new Date();
date.setTime(date.getTime()+(天* 24 * 60 * 60 * 1000));
expires =; expires = + date.toUTCString();
}
document.cookie =名称+ = +值+到期时间+; path = /;
}

函数readCookie(name){
var nameEQ = name + =;
var ca = document.cookie.split(’;’);
for(var i = 0; i< ca.length; i ++){
var c = ca [i];
while(c.charAt(0)==’’)c = c.substring(1,c.length);
if(c.indexOf(nameEQ)== 0)返回c.substring(nameEQ.length,c.length);
}
返回null;
}

函数deleteCookie(name){
createCookie(name, ,,-1);
}

var url = window.location.href;
var pathname = new URL(url).pathname;
var jsonObj = [];

//jsonObj.push(\"test);

var x = readCookie(’vid_cookies’);
if(x){
var res = x.split(,);
console.log(res);
for(var i = 0; i< res.length; i ++){
if(pathname!= res [i]){
alert( IS NOT EQUAL);
//res.push(pathname);
// var joinArray = res.join(,);
//console.log(joinedArray);
// createCookie(’vid_cookies',joinedArray,7);
// var z = readCookie(’vid_cookies’);
//console.log(z)
}
}
} else {
jsonObj.push(pathname);
createCookie(’vid_cookies',jsonObj,7);
}


//警报(jsonObj);

< / script>

这里的数组为:

  [ / evercookie-master / yahoo.html, / evercookie-master / facebook.html, / evercookie-master / facebook.html, / evercookie-master / facebook .html] 


解决方案

逻辑不正确。如果要仅在尚不存在的值中添加值,则必须先检查所有元素。



在您的代码中,只要元素的 any 不匹配,就添加该值。当然,总是这样,因为在 n 个元素中, n-1 个元素将不匹配。



一种方法是使用 Array#every

  if(res.every(x => x!==路径名)){
//添加到数组并设置cookie
}

或者,您可以将数组转换为 Set ,始终添加值并设置cookie。 Set 将自动对值进行重复数据删除:

  var res = new Set (x.split(,)); 
res.add(路径名);
res = Array.from(res);


What I want is to compare current url with my cookie array which would contain all the URL's a user has visited so it would compare that whether the array contains the current link or not so if not it would push that new link to the array and would again recreate the cookie with the new array which would contain the new pushed link so what I am facing right now is that everytime the if function which checks for the unique link always comes true I am not sure that what's the problem?

Can you people please have a look over it :

<script type="text/javascript">

function createCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

var url = window.location.href;
var pathname = new URL(url).pathname;
var jsonObj = [];

//jsonObj.push("test");

var x = readCookie('vid_cookies');
if (x) {
var res = x.split(",");
console.log(res);
for (var i = 0; i < res.length; i++) {
    if (pathname != res[i]) {
        alert("IS NOT EQUAL");
    //res.push(pathname);
    //var joinedArray = res.join(",");
    //console.log(joinedArray);
    //createCookie('vid_cookies',joinedArray,7);
    //var z = readCookie('vid_cookies');
    //console.log(z)
    }
}
} else {
    jsonObj.push(pathname);
createCookie('vid_cookies',jsonObj,7);
}


//alert(jsonObj);

</script>

Here is the Array as :

["/evercookie-master/yahoo.html", "/evercookie-master/facebook.html", "/evercookie-master/facebook.html", "/evercookie-master/facebook.html"]

解决方案

The logic is not correct. If you want to add a value to an array only if it doesn't exist yet, you have to check all elements before you add it.

In your code you are adding the value as soon as any of the element doesn't match. That will always be the case of course because out n elements, n - 1 will not match.

One way to do it would be to use Array#every:

if (res.every(x => x !== pathname)) {
 // add to array and set cookie
}

Alternatively you could convert the array to a Set, always add the value and set the cookie. The Set will automatically dedupe the values:

var res = new Set(x.split(","));
res.add(pathname);
res = Array.from(res);

这篇关于逻辑运算符始终为真吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆