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 store the removed items into new array after splice?

So, basically I'm trying to remove the item from an array of objects by index using splice, and it's working fine, but I would like also to store the removed collection into new Array, and sadly I'm only getting one element after clicking, and it's not giving me a collection;

const toArchive = (arr, index) => {
    arr.filter((elem, i) => {
        if (i === index) {
            return elem;
        }
    })
    let shallowCopy = [];
    shallowCopy.push(...arr.splice(index, 1));
    listToView(arr);
    console.log(shallowCopy);
}

This is what I'm getting:

[{…}]
[{…}]
[{…}]
[{…}]

The desired result:

(4) [{…}, {…}, {…}, {…}]

part of main code: http://jsfiddle.net/k8qa3cyp/1/

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

0

You will want to move the shallowCopy variable declaration outside of your function.

And console.log after the function was invoked.

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