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

220
Visualizações
how to check after deleting one element from array

hello i have creating a pragam that delete one array from another but. And it works fine i think but if i put 2 same value simultaneously, it remove only one value because of second argument of splice function. How can i check after deleting a element again if there any other number left my code is below.

const arr1 = [1, 2, 2, 3, 5, 2, 3, 7];
const arr2 = [2, 3];

let countArr = [];
for (let i = 0; i < arr2.length; i++) {
  for (let j = 0; j <= arr1.length; j++) {
    if (arr1[j] === arr2[i]) {
      arr1.splice(j, 1);
    }
  }
}
console.log(arr1);
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <script src="demo.js"></script>
</body>

</html>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

here it's pretty forward you can use 2 nested if conditions to check it again if the array still has that value

const arr1 = [1,2,2,3,5,2,3,7];
const arr2 = [2, 3];

for (let i = 0; i < arr2.length;i++){
  for (let j = 0; j < arr1.length; j++){
    if (arr1[j] === arr2[i]) {
      arr1.splice(j, 1)
      if (arr1[j] === arr2[i]) {
      arr1.splice(j, 1)
      }
    }
  }
}

or you can use set to remove any duplicatie values for the array before you begin looping it here you can read more about sets on MDN Documentation

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