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

121
Views
¿Cómo puedo actualizar el estado conservando la inmutabilidad?

No estoy usando la biblioteca inmutable y estoy trabajando en ello.

Quiero actualizar los valores de clave y valor de mapTable sin cambiar la constante de PRICE_OPTION2_STATE.

Te agradecería que me dijeras como. He publicado los resultados del problema y los resultados que quiero.

 //my code tableData['option2Price'] = 50000; const mapCopy = { ...copyState }; let tableCopy = { ...mapCopy[currentTableIndex].mapTable[findTableIndex] }; tableCopy = tableData; mapCopy.tableCopy = tableCopy; // problem console.log(PRICE_OPTION2_STATE); // initstate const PRICE_OPTION2_STATE = [ { id: 1, option2Name: '', option2LeftSelect: 'sell', mapTable: [ { tableId: 1, flag: true, option2Value: '', option2Price: '', discountInput2: '', discountOption2: 'won', option2Payment: '', option2Tax: '', option2settlementAmount: '', option2RightSelect: 'sell', }, ], }, ];

PRICE_OPTION2_STATE => resultado de la consola del problema

 [ { id: 1, option2Name: '', option2LeftSelect: 'sell', mapTable: [ { tableId: 1, flag: true, option2Value: '', option2Price: '', discountInput2: '', discountOption2: 'won', option2Payment: '50000', option2Tax: '', option2settlementAmount: '', option2RightSelect: 'sell', }, ], }, ]

resultado que quiero

 [ { id: 1, option2Name: '', option2LeftSelect: 'sell', mapTable: [ { tableId: 1, flag: true, option2Value: '', option2Price: '', discountInput2: '', discountOption2: 'won', option2Payment: '', option2Tax: '', option2settlementAmount: '', option2RightSelect: 'sell', }, ], }, ]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Asumiré que copyState es una referencia a PRICE_OPTION2_STATE o al menos es una especie de copia (superficial) de este. Entonces, para copiarlo, debes:

  • No use la notación de distribución de objetos en el nivel superior, ya que es una matriz.
  • no lo dejes en una copia superficial, sino cópialo profundamente . Por lo tanto, también deberá mapear la matriz interna y copiar los objetos que tiene.

Aquí hay algo de inspiración:

 // Assuming copyState has the structure of PRICE_OPTION2_STATE // Get a deep copy const mapCopy = copyState.map(({mapTable, ...rest}) => ({ ...rest, mapTable: mapTable.map(obj => ({...obj})) })); // Now this assignment will not impact copyState / PRICE_OPTION2_STATE mapCopy[currentTableIndex].mapTable[findTableIndex] = tableCopy;
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!