Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

88
Visualizações
Get values from nested object with dynamic ids - javascript

I'm recieving a payload from SlackAPI (block elements) and I can't get my head around oh how do I get data from it as ids and order always change. I want to get protection_fee.value, legal_fee.value and 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"
    }
  }
}

},

I tried Object.keys but obviously I failed as order changes.

current code:

      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 Respostas
Responde à pergunta

0

I would suggest creating a function like findProperty() that will find the relevant object in the payload.

We'd call Object.entries() on the payload.state.values object and then using Array.find() on the entry key/value pairs to find an object with the desired property.

Once we have the property we can return it.

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda