在这里完成新手问题。我试图循环一些数据,如果我检索的数据与数组中的项不匹配,则只运行一个函数。我将布尔变量设置为false,并且如果找到匹配项,则考虑将其值更改为“true”。如果我在为其赋值为“true”后直接记录我的变量,它会按预期工作。如果我将它记录在for循环之外,则会返回false。有关初学者的提示吗?谢谢!
var matchFound = false;
currentUserRequest.done(function (userData) {
for(var i = 0;i < arr.length;i++){
var item = arr[i];
console.log("request = " + request);
console.log("item.RequestName = " + item.RequestName);
console.log("current user = " + userData.d.Title);
console.log("item.Author[Title] = " + item.Author["Title"]);
if(request == item.RequestName && userData.d.Title == item.Author["Title"]){
matchFound = true;
//this is returning true as expected
console.log(matchFound);
}
}
});
//this is returning 'false', though I changed it to true above
console.log(matchFound);