Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda