使用angular.forEach的简单异步代码

时间:2023-02-05 02:32:32

flag is always false. how can I do?

旗帜永远是假的。我能怎么做?

code here:

var list = ['NOVALUE','VALUE','NOVALUE'];
var flag = false;
angular.forEach(list, function(value){
    if('VALUE' === value){
        flag = true;
    }
});
console.log(flag);

sorry, i missed something

对不起,我错过了什么

4 个解决方案

#1


1  

You should either use .indexOf(), as Alexander demonstrates:

您应该使用.indexOf(),正如Alexander所示:

var list = ['NOVALUE','VALUE','NOVALUE'];

var flag = list.indexOf('VALUE') !== -1;

console.log(flag);    

or .some() in the more general case:

或者.some()在更一般的情况下:

var list = ['NOVALUE','VALUE','NOVALUE'];

var flag = list.some(function (value) {
    return value === 'VALUE';
});

console.log(flag);

#2


0  

var list = ['NOVALUE','VALUE','NOVALUE'];
var flag = false;
angular.forEach(list, function(value){
    if (value === 'VALUE'){
        flag = true;
    }
    else{
        flag = false;
    }
    console.log(flag);
});

//false, true, false

#3


0  

Use break; so that whentrue statement comes it will go out of the loop

使用休息;所以当whentrue语句出现时它将退出循环

var list = ['NOVALUE','VALUE','NOVALUE'];
        var flag = false;
        for(var i=0;i<list.length;i++){
        if('VALUE' === value)
         {
          flag=true;                
          break;
          }else
          flag=false;
        }
        console.log(flag);

#4


0  

Because the last value in list is "NOVALUE" so it is always false. You can do

因为列表中的最后一个值是“NOVALUE”所以它始终为false。你可以做

flag = list.indexOf('VALUE') > -1

#1


1  

You should either use .indexOf(), as Alexander demonstrates:

您应该使用.indexOf(),正如Alexander所示:

var list = ['NOVALUE','VALUE','NOVALUE'];

var flag = list.indexOf('VALUE') !== -1;

console.log(flag);    

or .some() in the more general case:

或者.some()在更一般的情况下:

var list = ['NOVALUE','VALUE','NOVALUE'];

var flag = list.some(function (value) {
    return value === 'VALUE';
});

console.log(flag);

#2


0  

var list = ['NOVALUE','VALUE','NOVALUE'];
var flag = false;
angular.forEach(list, function(value){
    if (value === 'VALUE'){
        flag = true;
    }
    else{
        flag = false;
    }
    console.log(flag);
});

//false, true, false

#3


0  

Use break; so that whentrue statement comes it will go out of the loop

使用休息;所以当whentrue语句出现时它将退出循环

var list = ['NOVALUE','VALUE','NOVALUE'];
        var flag = false;
        for(var i=0;i<list.length;i++){
        if('VALUE' === value)
         {
          flag=true;                
          break;
          }else
          flag=false;
        }
        console.log(flag);

#4


0  

Because the last value in list is "NOVALUE" so it is always false. You can do

因为列表中的最后一个值是“NOVALUE”所以它始终为false。你可以做

flag = list.indexOf('VALUE') > -1