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

154
Views
¿Cómo combinar forEach loop con every para hacerlo más eficiente y organizado?

así que aquí está el código que tengo para cada uno y pongo una gran cantidad de datos en una matriz y luego vuelvo a repetir y verifico cada $. El objetivo es hacer un bucle `` así

 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

No tiene sentido usar .every() porque ese método devuelve un valor booleano y el valor de retorno no se usa en el código.

Tampoco tiene sentido probar la longitud del texto con expectativa, porque la condición ya confirma que todo el texto tendrá más de 5 caracteres (también más de 50 caracteres).

En la mayoría de las aplicaciones, está bien recorrer una matriz varias veces. Por lo general, no afecta el rendimiento de manera notable.

Para responder a su pregunta ahora, si desea iterar una vez, es posible llamando directamente al método expect en el ciclo forEach en lugar de filtrar y luego iterar.

 $$('[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!