[英]Last iteration of for loop if (something equals something)
This code works, but gives me results of all cases when Item === CheckId
in the array. 这段代码可以工作,但是当数组中的
Item === CheckId
时,可以给出所有情况的结果。 How to get only last time? 如何只获得最后一次?
(Item === CheckId).length
results in undefined. (Item === CheckId).length
导致未定义。 Also I tried to create variable outside of if
statement and increase it inside of if
statement. 我也尝试在
if
语句外创建变量,并在if
语句内增加变量。 But it increased only once. 但是它只增加了一次。
function updateDirections() {
$('.ui-selected').each(function(i, obj) {
textArea = $("#fname").val();
textArea = textArea.split(';');
//textArea.replace(/\(.+?,\ \{/, "");
//console.log((textArea[4].toLowerCase().indexOf(this.id) >= 0));
//textArea = textArea.map(function(w){ return +!!~this.id.indexOf(w) });
//console.log(obj.id);
Item = this.id;
var arrayLength = textArea.length;
for (var i = 0; i < arrayLength; i++) {
CheckId = textArea[i];
CheckId = CheckId.match(/\(.+?,\ \{/)
CheckId = String(CheckId).replace(/\(/g, "").replace(/,/g, "").replace(/ /g, "").replace(/\{/g, "");
if (Item === CheckId) {
console.log(textArea[i]);
}
}
});
}
I'm not sure of what you want to achieve, but if you want the value of the last console.log
, you can indeed use a variable : 我不确定要实现的目标,但是如果要获取最后一个
console.log
的值,则可以使用变量:
Item = this.id;
var arrayLength = textArea.length,
lastValue = null; // here is the variable
for (var i = 0; i < arrayLength; i++) {
CheckId = textArea[i];
CheckId = CheckId.match(/\(.+?,\ \{/)
CheckId = String(CheckId).replace(/\(/g, "").replace(/,/g, "").replace(/ /g, "").replace(/\{/g, "");
if (Item === CheckId) {
lastValue = textArea[i];
}
}
// do anything you want with lastValue
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.