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

104
Views
Cómo obtener el estado final de una matriz de objetos que representan cambios

Tengo una serie de cambios (objetos) y me gustaría obtener el estado final después de que se hayan realizado todos los cambios.

Por ejemplo, tener esta serie de cambios:

 const today = new Date(); const yesterday = new Date(today); yesterday.setDate(yesterday.getDate() - 1); const tomorrow = new Date(today); tomorrow.setDate(tomorrow.getDate() + 1); const item1 = { id: 'id1', timestamp: yesterday, }; const item2 = { id: 'id2', timestamp: today, }; const item3 = { id: 'id3', timestamp: tomorrow, }; const changes = [ { action: 'swap', item1, item2, }, { action: 'swap', item1, item2: item3, }, ]

Estoy esperando esta matriz con el estado final de cada elemento:

 const finalState = [ { id: item1.id, timestamp: item3.timestamp, }, { id: item2.id, timestamp: item1.timestamp, }, { id: item3.id, timestamp: item2.timestamp, }, ]

Actualmente, la lógica que estoy usando es esta. Sin embargo, no está funcionando correctamente.

 export const convertChangesToFinalState = ({ changes, }): FinalChanges => { const finalState: { [id: string]: FinalChange; } = {}; for (const { item1, item2 } of changes) { finalState[item1.id] = { id: item1.id, timestamp: new Date(finalState[item2.id]?.timestamp ?? item2.timestamp), }; finalState[item2.id] = { id: item2.id, timestamp: new Date(finalState[item1.id]?.timestamp ?? item1.timestamp), // timestamp: new Date(new Date(item2.timestamp).getTime() !== new Date(finalState[item1.id]?.timestamp).getTime()? finalState[item1.id]?.timestamp : item1.timestamp), }; } return Object.values(finalState); };

¿Podría por favor ayudarme a resolver esto?

¡Gracias de antemano!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Cuando el item1 se almacena en finalChanges , no puede volver a leer este objeto modificado, de lo contrario, accederá al nuevo valor actualizado. Deberá crear una copia del objeto no modificado para cada iteración antes de modificarlo.

Si no existe, solo obtenga el valor que queremos establecer del elemento principal.

 export const convertChangesToFinalState = ({ changes, }): FinalChanges => { const finalState: { [id: string]: FinalChange; } = {}; for (const { item1, item2 } of changes) { const notAlteredItem = { ...finalState }; finalState[item1.id] = { id: item1.id, timestamp: new Date(finalState[item2.id]?.timestamp ?? item2.timestamp), }; finalState[item2.id] = { id: item2.id, timestamp: new Date(notAlteredItem[item1.id]?.timestamp ?? item1.timestamp) }; } return Object.values(finalState); };

Consulte este entorno limitado https://codesandbox.io/s/zen-johnson-dkxk0?file=/src/index.js

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!