Pavel Vanchugov
Pavel Vanchugov

Reputation: 393

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. How to get only last time? (Item === CheckId).length results in undefined. Also I tried to create variable outside of if statement and increase it inside of if statement. 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]);
                            }

                        }


                    });
                }

Upvotes: 0

Views: 548

Answers (1)

Brewal
Brewal

Reputation: 8189

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 :

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

Upvotes: 1

Related Questions