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

193
Views
jQuery each() loop no devuelve el control cuando se cumple una condición

Quiero devolver verdadero/falso de mi función cuando se cumpla la condición if . Sin embargo, no devuelve true , cada vez que devuelve false . Por favor, ayúdame. Gracias.

 function catExists(cName) { $("#room_has_cat_table tbody tr td:first-child").each(function(index, item) { var existingCat = $(this).text(); alert(existingCat); if (existingCat == cName) { return true; } }); return false; }
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

El problema con su lógica es que no puede return un valor de una función anónima.

Para corregir su lógica, defina una variable booleana que pueda actualizar dentro del bucle cuando se encuentre una coincidencia:

 function foo(cName) { let matchFound = false; $("#room_has_cat_table tbody tr td:first-child").each(function() { var existingCat = $(this).text(); if (existingCat == cName) { matchFound = true; return; // exit the each() loop } }); return matchFound; }

Sin embargo , un mejor enfoque sería usar filter() para encontrar la coincidencia. Esto evita la necesidad del bucle explícito:

 let matchFound = $("#room_has_cat_table tbody tr td:first-child").filter((i, el) => el.innerText.trim() === cName).length !== 0;
about 4 years ago · Santiago Gelvez 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!