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

192
Views
¿Por qué no puedo eliminar elementos de la matriz?

Esta función debería eliminar los elementos menores que 1 y mayores que 4, pero no lo hace. La matriz sigue siendo la misma.

 let arr = [5, 3, 8, 1]; function filterRangeInPlace(arr, a, b) { arr.forEach((item, index) => { if (a > item && b < item) { arr.splice(index, 1); }; }); }; filterRangeInPlace(arr, 1, 4); console.log(arr);

¿Qué ocurre?

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

0

¿Cuándo sería un elemento individual menor que 1 y mayor que 4? Si desea eliminar elementos que son menos de 1 o más de 4, debe hacer lo siguiente:

 if (a > item || b < item)
about 4 years ago · Juan Pablo Isaza Report

0

estás invirtiendo los argumentos al llamar a la función. deberías hacer

 filterRangeInPlace(arr, 4, 1)

about 4 years ago · Juan Pablo Isaza Report

0

básicamente está manteniendo los elementos entre el rango dado en lugar de eliminar los elementos, vaya a mantener los elementos, esto devolverá lo mismo y también use la función de filter , básicamente itera a través de la matriz y, al igual que foreach , hay dos parámetros dentro de su elemento e índice, la única diferencia es que el filtro mantiene el valor cuando usted devolver verdadero o ignorar si devuelve falso

puede consultar más sobre el filtro aquí filtrar

 let arr = [5, 3, 8, 1]; function filterRangeInPlace(arr, a, b) { return arr.filter(each=>{ return each > a && each < b }) }; arr = filterRangeInPlace(arr, 1, 4); console.log(arr);

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!