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

191
Views
¿Cómo hacer que la función de filtro no se detenga al principio sea falsa?

Estoy tratando de filtrar una matriz de objetos comparándola con otra matriz recorriendo sus valores. El problema es que la función de filtro se detiene en el primer falso de forma predeterminada, por lo que si el bucle devuelve (falso, verdadero, falso) no pasará el filtro.

¿Hay alguna forma de evitar esto?

código (he puesto matrices y objetos simplificados en el código para el ejemplo):

 const jobs = [ {id: 1, location: 'AA'}, {id: 2, location: 'BB'}, {id: 3, location: 'CC'}, ] const filteredLocations = ['AA', 'CC'] const filteredJobs = jobs.filter(function(el, i) { do { return el.location === filteredLocations[i]; } while (filteredLocations.length >= i) }) // this only returns the first object instead of the desired first and third

¡Gracias!

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

Use la función .filter como se pretende, cualquier devolución se agregará a la matriz devuelta.

 const jobs = [ {id: 1, location: 'AA'}, {id: 2, location: 'BB'}, {id: 3, location: 'CC'}, ] const filteredLocations = ['AA', 'CC'] const filteredJobs = jobs.filter(el => { return filteredLocations.includes(el.location); });

nota al margen, su código tiene 2 problemas:

  1. puede devolver falso dentro del bucle while, si no coincide con la primera aparición, por lo tanto, ¿por qué la única salida que tenía?
  2. comparas filteredLocations.length con la i (índice) del bucle de matriz de jobs , los jobs y las filteredLocations son 2 matrices diferentes, por lo que no tiene sentido hacerlo
about 4 years ago · Santiago Gelvez Report

0

El filtro no se detiene al principio falso. Funciona correctamente pero no puede devolver el primer y el tercer objeto porque el tercer objeto está en el índice 2 y no tiene ningún elemento en filteredLocations en ese índice.

about 4 years ago · Santiago Gelvez Report

0

Le aconsejo que reemplace el bucle do - while con una función de búsqueda simple de la siguiente manera:

 const jobs = [ {id: 1, location: 'AA'}, {id: 2, location: 'BB'}, {id: 3, location: 'CC'}, ] const filteredLocations = ['AA', 'CC'] const filteredJobs = jobs.filter(function(el, i) { return filteredLocations.find((filteredLocation) => filteredLocation === el.location); }) console.log(filteredJobs);

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!