Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

160
Vistas
Remove item from an array using a function

I'm trying to build a function that removes an item from an array. Both the array and item are configured using parameters pushing in when I call the function.

However it's not returning the expected [1,2,4] rather it's returning "not yet" a string I built into an if statment to return if it fails.

I can see in a console log the popped variable = 3 and the current for loop is correctly looping through all the options. So why isn't it working?

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 Respuestas
Responde la pregunta

0

Ok I've commented your code, the problems in it and made changes accordingly so that it works like you wanted it to:

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)

The main problem is that args does not contain what you thought it does (the numbers array), it is actually args[0] that does.

The other thing was that when you found the value you wanted to remove from the array, you never actually removed it. So here we use splice to actually remove the value before returning.

about 4 years ago · Juan Pablo Isaza Denunciar

0

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

0

I don't know why you don't use any build-in function of JS like

let removeFromArray = (arr, remove) => arr.filter(x => x != remove)

let filteredArray = removeFromArray([1, 2, 3, 4], 3)

But let's do it your way

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda