簡體   English   中英

for循環的最后一次迭代if(某物等於某物)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM