In Cypress loop return true if condition satisfies, else return false after the iteration ends

I need to run a loop through multiple elements having the same CSS selector and return true if the element.text() matches with the string. If there is no match then return false at the end.
I tried something like below but it didn’t work:

 getProfilePresentOrNot(profileName){
        var flag=0;
        cy.get('li>div>div>span').each(($el,index,$list)=>{
            if($el.text().includes(profileName)) {
                flag=1;
            }
        });
    return flag;
    }

This function always returns 0 even though I can confirm that the condition in the if block satisfies.

Kindly advise. Thanks

Source: Ask Javascript Questions

LEAVE A COMMENT