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

86
Views
Obtenga valores de objetos anidados con identificadores dinámicos - javascript

Recibo una carga útil de SlackAPI (elementos de bloque) y no entiendo cómo obtengo datos de ella, ya que las identificaciones y el orden siempre cambian. Quiero obtener protection_fee.value, legal_fee.value y repayment_date.selected_date

 "state": { "values": { "CjV": { "protection_fee": { "type": "plain_text_input", "value": "111" } }, "36tAM": { "legal_fee": { "type": "plain_text_input", "value": "111" } }, "oH8": { "repayment_date": { "type": "datepicker", "selected_date": "1990-04-18" } } }

},

Intenté Object.keys pero obviamente fallé debido a cambios en el orden.

código actual:

 const payload = JSON.parse(body); const state = payload.state.values; const first = Object.keys(state)[0]; const second = Object.keys(state)[1]; const repaymentDate = state[first].protection_fee.value; const protectionFee = state[second].legal_fee.value;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Sugeriría crear una función como findProperty() que encuentre el objeto relevante en la carga útil.

Llamaríamos a Object.entries() en el objeto payload.state.values y luego Array.find() en los pares clave/valor de entrada para encontrar un objeto con la propiedad deseada.

Una vez que tengamos la propiedad podemos devolverla.

 let payload = { "state": { "values": { "CjV": { "protection_fee": { "type": "plain_text_input", "value": "111" } }, "36tAM": { "legal_fee": { "type": "plain_text_input", "value": "111" } }, "oH8": { "repayment_date": { "type": "datepicker", "selected_date": "1990-04-18" } } } } } function findProperty(obj, key) { const [, value] = Object.entries(obj).find(([k,v]) => v[key]); return value[key]; } console.log('legal_fee:', findProperty(payload.state.values, 'legal_fee').value) console.log('protection_fee:', findProperty(payload.state.values, 'protection_fee').value)
 .as-console-wrapper { max-height: 100% !important; }

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!