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

166
Views
Eliminar elemento de una matriz usando una función

Estoy tratando de construir una función que elimine un elemento de una matriz. Tanto la matriz como el elemento se configuran usando parámetros que se presionan cuando llamo a la función.

Sin embargo, no devuelve el [1,2,4] esperado, sino que devuelve "todavía no", una cadena que incorporé en una declaración if para devolver si falla.

Puedo ver en un registro de consola la variable emergente = 3 y el bucle for actual está recorriendo correctamente todas las opciones. Entonces, ¿por qué no funciona?

 const removeFromArray = function() { let args = Array.from(arguments); let popped = args.pop(); for (i = 0; i < args.length; i++) { let current = args[i]; if (current === popped) { console.log(args); return args; } else { console.log("not yet"); } } }; removeFromArray([1, 2, 3, 4], 3);
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Ok, comenté su código, los problemas que contiene y realicé los cambios correspondientes para que funcione como usted quería:

 const removeFromArray = function() { // arguments is not [1, 2, 3, 4, 3], but instead it's [[1, 2, 3, 4], 3] (length is 2, remember this later) let args = Array.from(arguments); // pop works correctly and returns 3 let popped = args.pop(); // here we cannot loop with args.length, as it is 2 // if we change args.length to args[0].length, this will work for (i = 0; i < args[0].length; i++) { // args[i] won't work here for the same reason args.length didn't work, // because we're targeting a wrong thing // if we change this to args[0][i], it will work let current = args[0][i]; // After the changes, this if will work correctly if (current === popped) { // We can't just return args // A) we're once again targeting and wrong thing // B) we haven't removed anything yet // so lets change this to first splice the array (remove the wanted value) args[0].splice(i, 1); // and then return the array where the wanted value is removed return args[0]; } } }; const newArray = removeFromArray([1, 2, 3, 4], 3); // output the returned new array where 3 is removed console.log(newArray)

El principal problema es que args no contiene lo que pensabas que contiene (la matriz de números), en realidad es args[0] el que lo contiene.

La otra cosa fue que cuando encontró el valor que quería eliminar de la matriz, en realidad nunca lo eliminó. Así que aquí usamos el empalme para eliminar el valor antes de regresar.

about 4 years ago · Juan Pablo Isaza Report

0

 const removeFromArray = function (array, itemToRemove) { return array.filter(item => item !== itemToRemove); };
about 4 years ago · Juan Pablo Isaza Report

0

No sé por qué no usas ninguna función integrada de JS como

 let removeFromArray = (arr, remove) => arr.filter(x => x != remove) let filteredArray = removeFromArray([1, 2, 3, 4], 3)

Pero hagámoslo a tu manera

 const removeFromArray(arr, remove) { const items = []; for (const item of arr) { if (item != remove) items.push(item) } return items; }; removeFromArray([1, 2, 3, 4], 3);
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!