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

65
Views
Recorrer un objeto solo devuelve el primer objeto

Estoy tratando de remodelar algunos datos para usarlos en una tabla. Para hacer esto, estoy mapeando una matriz y luego recorriendo un objeto dentro de ella para crear un nuevo objeto con los datos que quiero en él. El problema que tengo es que si el objeto contiene más de un objeto dentro, solo obtengo un objeto nuevo para el primer elemento.

Estoy tratando de obtener un nuevo objeto para cada uno de los objetos contenidos dentro changedProperties

¿Alguien puede ayudar? / ¿Explique?

Aquí hay un ejemplo de trabajo:

 const MOCK_DATA = [ { triggeredByID: "d5ae18b7eb6f", triggeredByName: "name", changedProperties: { LastName: { __oldValue: "Mallory", __newValue: "Mallorie", }, Suffix: { __oldValue: "DO", __newvValue: "NP", }, }, createdAt: "2022-06-01T10:20:21.329337652Z", }, { triggeredByID: "d5ae18b7eb6f", triggeredByName: "John", changedProperties: { State: { __oldValue: ["TX", "AL"], __newValue: ["TX", "AL", "FL"], }, City: { __oldValue: "Austin", __newValue: "San Antonio", }, }, createdAt: "2022-06-01T10:20:21.329337652Z", }, ]; const changedProperties = MOCK_DATA.map((item, idx) => { for (const [key, value] of Object.entries(item.changedProperties)) { return { field: key, old: item.changedProperties[key].__oldValue, new: item.changedProperties[key].__newValue, name: item.triggeredByName, createdAt: item.createdAt, }; } }); console.log(changedProperties)

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

0

Sí, si regresa dentro de un bucle for, su ámbito es la función. Lo que significa que regresará de la función completa en la primera iteración de su ciclo. Prueba esto en su lugar:

 const changedProperties = MOCK_DATA.map((item, idx) => { return Object.entries(item.changedProperties).map(([key, value]) => { return { field: key, old: item.changedProperties[key].__oldValue, new: item.changedProperties[key].__newValue, name: item.triggeredByName, createdAt: item.createdAt, }; } }); console.log(changedProperties)
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!