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

233
Views
Deje de filtrar la matriz si la condición no se cumple en el siguiente índice, aunque todavía hay datos que pueden satisfacer el siguiente índice

Quiero saber cómo recuperar datos de los resultados del filtro donde el proceso de filtro se detiene automáticamente si no se cumplen las condiciones en el siguiente índice cuando se filtra. Aquí hay un ejemplo del código que hice.

 var array = [1,2,3,4,5,100, 12,13,14] var filterArr = array.filter((value, index) => { var nextValue = array[index+1] if(value >= 20 && nextValue <= 20){ return true } return false }) //result that i want in filterArr variable is [1,2,3,4,5]

Y el resultado que quiero es así ignorando los valores 11, 12, 13, 14 porque hay un valor en el siguiente índice que es 100 que es mayor que 20

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

En lugar de usar .filter() , puede usar .slice() con .findIndex() . Primero, puede encontrar el índice del elemento 100 usando .findIndex() , y luego usando .slice() puede mantener todos los elementos hasta ese índice, pero sin incluirlo:

 const array = [1, 2, 3, 4, 5, 100, 12, 13, 14]; const idx = array.findIndex((value, i) => value >= 20 && array[i+1] <= 20); const res = idx > -1 ? array.slice(0, idx) : array.slice(); // res is a copy of the array, potentially with some items removed console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

Puede tomar una bandera en un cierre y omitir los siguientes valores del filtrado si la bandera es false .

 const array = [1, 2, 3, 4, 5, 100, 12, 13, 14], result = array.filter( (flag => value => flag && (flag = value <= 20)) (true) ); console.log(result); // [1, 2, 3, 4, 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!