[英]Last iteration of for loop if (something equals something)
這段代碼可以工作,但是當數組中的Item === CheckId
時,可以給出所有情況的結果。 如何只獲得最后一次? (Item === CheckId).length
導致未定義。 我也嘗試在if
語句外創建變量,並在if
語句內增加變量。 但是它只增加了一次。
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]);
}
}
});
}
我不確定要實現的目標,但是如果要獲取最后一個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.