E
E
Eugene2019-01-19 18:46:20
JavaScript
Eugene, 2019-01-19 18:46:20

How to get out of a for loop?

How to exit a loop when a condition is met?

for(let i = 0; i < elements.length; i++){
        elements[i].findElement(by.xpath('.//span')).getAttribute('innerText').then(function(text){
            if(~'Бадминтон'.indexOf(text)){
                console.log(text);
                // Выход из цикла перебора
            };
        });
    };

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artray, 2019-01-19
@you_are_enot

async function
for...of

(async () => {
    // ...
    
    
    for(const element of elements) {
        const text = await element.findElement(by.xpath('.//span')).getAttribute('innerText');

        if(~'Бадминтон'.indexOf(text)) {
            console.log(text);
            break;
        }
    }
    
    // ...
})()

A
Alexey, 2019-01-20
@mrAlexRabota

let isValid = true;
for( let i = 0; isValid && i < array.length; i++) {
// do something
// if you wanna break then set isValid = false;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question