Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
How to combine forEach loop with every to make it more efficient and organized?

so here is code i have forEach and im putting a lot of data in an array and then looping again and verify every $. THe goal is to make one loop `` like so

it('should be able to search rolex in ebay and every price should have $ in it', () => {
        browser.url('./')
        $('[class="gh-tb ui-autocomplete-input"]').click();
        $('[class="gh-tb ui-autocomplete-input"]').setValue("rolex");
        browser.keys("Enter");
        const searchResult = []
        $$('[class="s-item__title"]').forEach((element) => {
            if (element.getText().length > 50) searchResult.push(element.getText().toLowerCase());
        });
        searchResult.every((i) => expect(i).to.have.lengthOf.above(5))
        const priseOfSearch = []
        $$('[class="s-item__price"]').forEach((element) => {
            if (element.getText().length > 0) priseOfSearch.push(element.getText().toLowerCase());
        });
        priseOfSearch.every((i) => expect(i).to.contain('$'));

new verion

it('should be able to search rolex in ebay and every price should have $ in it', () => {
        browser.url('./')
        $('[class="gh-tb ui-autocomplete-input"]').click();
        $('[class="gh-tb ui-autocomplete-input"]').setValue("rolex");
        browser.keys("Enter");
        $$('[class="s-item__title"]').forEach((element) => {
            if (element.getText().length > 50) {
                expect(element.getText().toLowerCase().to.have.lengthOf.above(5))
            }
            $$('[class="s-item__price"]').forEach((element) => {
                if (element.getText().length > 0) {
                    expect(element.getText().toLowerCase().to.contain('$'));
                }
            });
        })
    })
})   
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It does not make sense to use .every() because that method returns a boolean and the return value is not being used in the code.

It also does not make sense to test the length of the text with expect, because the condition already confirms that all text will be longer than 5 characters (also longer than 50 characters).

In most applications it's fine to loop over an array multiple times. It does not usually impact performance in a noticeable way.

To answer your question now, if you want to iterate once it's possible by calling the expect method in the forEach loop directly instead of filtering and then iterating.

$$('[class="s-item__title"]').forEach((element) => {
  if (element.getText().length > 50) {
    expect(element.getText().toLowerCase()).to.have.lengthOf.above(5)
  }
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!