Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!