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

131
Visualizações
How to implement a function that takes an array and some other arguments then removes the other arguments from that array

Please guys, this is an exercise (exercise 04) from Odin Project. I don't know if there is anything wrong with the code below, it keeps failing the 'npm test' (a test in the terminal) but then i checked it with console.log and it worked.

const removeFromArray= function(...args) {
    const array = args[0];
        const newArray = [];
             array.forEach((item) => {
                 if(!args.includes(item)) {
                      newArray.push(item);  
        }
        });
        return newArray;
    };
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It seems to me that I have not understood the question very well. But JS has built-in functions that serve the purpose

Array map https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Array filter https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

But from what I see in the code you have to remove the first element from the array.

I suggest you take a look at this question

remove first element from array and return the array minus the first element

about 4 years ago · Juan Pablo Isaza Relatório

0

I'd suggest using Array.filter() in combination with Array.includes() to achieve the required result.

We filter any element from the input array if it is present in the itemsToRemove array.

function removeFromArray(array, itemsToRemove) {
    return array.filter(element => !itemsToRemove.includes(element));
}

let array = [1,2,3,4,5,6];
let itemsToRemove = [2,4,6];

console.log(removeFromArray(array, itemsToRemove))

We can do this in a more efficient manner if itemsToRemove is very large, using a Set:

function removeFromArray(array, itemsToRemove) {
    const itemsToRemoveSet = new Set(itemsToRemove);
    return array.filter(element => !itemsToRemoveSet.has(element));
}

let array = [1,2,3,4,5,6];
let itemsToRemove = [2,4,6];

console.log(removeFromArray(array, itemsToRemove))

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