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

647
Views
El objeto no es un error extensible al crear un nuevo atributo para una matriz de objetos

Tengo una función que necesita extender una matriz de javascript, incluido un nuevo atributo llamado selected :

 export const initSelect = (data) => { let newData = data.concat(); newData.map((item) => { item.selected = false; }) return newData; }

data es un valor de estado de ReactJS (proviene de this.state.data cuando se llama a la función), pero esto no parece ser un problema ya que newData es una nueva copia de la matriz de data ...

Recibo el siguiente error:

 TypeError: Cannot add property selected, object is not extensible
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Probablemente necesites copiar los objetos:

 export const initSelect = (data) => { return data.map((item) => ({ ...item, selected: false })); }
over 4 years ago · Santiago Trujillo Report

0

No puede extender el item con la propiedad selected , y su matriz es solo una copia superficial.

Si desea poder extender, tendrá que hacer una copia profunda de su matriz. Puede ser suficiente con:

 let newData = data.map((item) => Object.assign({}, item, {selected:false}) )
over 4 years ago · Santiago Trujillo Report

0

data = JSON.parse(JSON.stringify(data)));
over 4 years ago · Santiago Trujillo 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!