Javascript 为什么我的代码仍然会返回true?

Javascript 为什么我的代码仍然会返回true?,javascript,Javascript,下面是我的代码 test.map((value, index) => { console.log(value); if(value.customer_mobile) { alert(index + 'fail'); return false; } if(value.customer_email) { alert(index + 'fail'); return false; } })

下面是我的代码

test.map((value, index) => {
    console.log(value);
    if(value.customer_mobile) {
        alert(index + 'fail');
        return false;
    }

    if(value.customer_email) {
        alert(index + 'fail');
        return false;
    }
})

return true;

我从上面运行代码,即使
customer\u mobile
为false,它也会返回true。为什么?

试试这个:

var flag = true;
test.map((value, index) => {
    console.log(value);
    if(customer_mobile) {
        alert(index + 'fail');
        flag = false;
    } 

    if(customer_email) {
        alert(index + 'fail');
        flag = false;
    }
});

return flag;

它不起作用,因为当您使用Array.map将测试数组映射到布尔数组时

如果您将其分配给一个变量,您将看到

var temp = test.map((value,index)=> {
     /...
});
console.log(temp) // [false,false]
然后你就返回真值

因此,您应该首先在map回调函数的末尾返回true。然后检查返回的数组是否为真

return test.map((value, index) => {
    console.log(value);
    if(value.customer_mobile) {
        alert(index + 'fail');
        return false;
    }
    if(value.customer_email) {
        alert(index + 'fail');
        return false;
    }
    return true;
}).every(bool => bool)

fail
后出现键入错误,缺少结束单引号。很抱歉,我已修复键入错误
customer\u mobile
=>
值。customer\u mobile
,我认为这是数组元素的属性。。。共享
test的值
既然您修改了代码,为什么不
console.log(customer\u mobile)
?-请发布一个我从上面运行代码的帖子,即使customer_mobile为false,它也会返回true。为什么?如果将customer\u mobile设置为false,它将永远不会进入if条件。在使用标志时,您应该只使用test.forEach,因为您没有映射任何测试数组。