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

145
Visualizações
How to filter array by array using javascript with duplicate key

I have 2 array and i will filtered but key duplicate.

const arr1 = [1, 2, 3, 3, 3, 4];
const arr2 = [1, 3];

function filteredArray(arr1, arr2) {...}

const newArr = filteredArray(arr1, arr2);

console.log(newArr);

// result: [2, 3, 3, 4];
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can check the below implementation. We should not modify the original data as well, that's why I've cloned your array for the array modification instead of changing items on the original one.

const arr1 = [1, 2, 3, 3, 3, 4];
const arr2 = [1, 3];

function filteredArray(arr1, arr2) {
  const newArr1 = [...arr1] //clone the array to keep the original array data
  const set2 = new Set(arr2)
  for(let i = 0; i < newArr1.length; i++) {
     if(set2.has(newArr1[i])) {
        set2.delete(newArr1[i])
        newArr1.splice(i,1)
     }
  }
  return newArr1
}

const newArr = filteredArray(arr1, arr2);

console.log(newArr);

about 4 years ago · Juan Pablo Isaza Relatório

0

Use set and filter

function filteredArray(arr1, arr2) {
  const set = new Set(arr2);
  return arr1.filter(item => !set.delete(item));
}

const arr1 = [1, 2, 3, 3, 3, 4];
const arr2 = [1, 3];

console.log(filteredArray(arr1, arr2));

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