Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

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

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda