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

140
Visualizações
How to splice an object into an array of objects?

What is the best way to update an array of objects with new data?

I'm currently trying to update an array of objects using splice. However, when the below code is executed I get an error saying: "TypeError: Cannot add property 2, object is not extensible".

I'm following the Mozilla documentation for 'splice' and I'm 99% sure I'm doing that part right. The problem seems to be coming from the fact that I'm working with objects. Should I be doing things a different way or following different documentation?

//console.log(designs)
//output:
// (2) [{...}, {...}]

    const file = "file-name-example"
    let temp = designs.splice(currentIndex, 0, {
        image: file,
        positions: [0,0,1] 
    })
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The issue is that you are trying to modify an array that has been frozen/sealed (via Object.freeze or Object.seal). Therefore, when the splice method tries to add another property named 3, it cannot as the object has been locked off from modifications. You need to remove the locking code to be able to modify your array again.

Reproduction:

const names = ["Joe", "Jon"];
Object.seal(names);
names.splice(1, 0, "Ben"); // Uncaught TypeError: Cannot add property 2, object is not extensible

const names2 = [...names];
Object.freeze(names);
names.splice(1, 0, "Ben"); // Uncaught TypeError: Cannot add property 2, object is not extensible

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