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

171
Visualizações
How to remove a particular item from an array?

My Dom is like this :

<div class="child">
  child1
  <button class="btn">Delete</button>
</div>
<div class="child">
  child2
  <button class="btn">Delete</button>
</div>
<div class="child">
  child3
  <button class="btn">Delete</button>
</div>
<div class="child">
  child4
  <button class="btn">Delete</button>
</div>

Now my js like this:

const btn = document.querySelectorAll('.btn');
btn.forEach((button, i) => {
  button.addEventListener('click', () => {
    const boxes = document.querySelectorAll('.child');
    boxes.splice(i, 1);
  });
});

Now boxes is an array and after clicking on delete button I want to remove The pertucal item from that arrray. but it throws the following error:

boxes.splice() is not a function

How can I solve this?

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

0

splice() is an array method. The collection returned from querySelectorAll() is Array-like. Use something like ... spread operator to convert it to an array.

const btn = document.querySelectorAll('.btn');
btn.forEach((button, i)=>{
    button.addEventListener('click', ()=>{
        const boxes = [...document.querySelectorAll('.child')];
        boxes.splice(i, 1)
    })
})

Note: This doesn't actually delete anything from the DOM. Only removes it from the array as you have indicated in the question.

about 4 years ago · Juan Pablo Isaza Relatório

0

The querySelectorAll returns a noed list You have to convert it to an array

const nodelist = document.querySelectorAll('div');
const nodelistToArray = Array.apply(null, nodelist);
about 4 years ago · Juan Pablo Isaza Relatório

0

Now boxes is an array and ...

Actually, boxes is NOT an array, as you think. boxes is the result of document.querySelectorAll, which returns a NodeList, not an array. A NodeList is very similar to an array, but it doesn't have all of the methods of Array.prototype. To use .splice on a NodeList, you have to convert it to an array. Here is how you would do that:

const btn = document.querySelectorAll('.btn');
btn.forEach((button, i) => {
  button.addEventListener('click', () => {
    const boxes = Array.from(document.querySelectorAll('.child'));
    console.log(boxes.splice(i, 1));
  });
});
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