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

110
Visualizações
How to use Lodash get function to get the nested array objects

I need to get the array of object values without using foreach by using lodash get function

"PaymentTypes": [
    {
      "categoryName": "Payment Type 1",
      "paymentOptions": [{"payCode": "PType1"}]
    },
    {
      "categoryName": "Payment Type 2",
      "paymentOptions": [{"payCode": "PType2"},{"payCode": "PType3"}]
    },
    {
      "categoryName": "Payment Type 3",
      "paymentOptions": [{"payCode": "PType4"}]
    }
]

I want to have all the paycode in single variable like :

const payCode ={"PType1","PType2","PType3","PType4"};

I have used lodash get function to get values using category name but not sure how to get the paycode type for each category in a single object using lodash get function

const category = get(PaymentTypes,"PaymentOptions");
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Use the Array.map function instead.

const payCodes = paymentTypes.map(o => o.payCode);
about 4 years ago · Juan Pablo Isaza Relatório

0

I'd suggest using a combination of Array.flatMap() and Array.map().

We'd call .flatMap() on the PaymentTypes array, then call .map() on the paymentOptions array to get all payCodes:

const obj = { "PaymentTypes": [{ "categoryName": "Payment Type 1", "paymentOptions": [{ "payCode": "PType1"}] }, { "categoryName": "Payment Type 2", "paymentOptions": [{ "payCode": "PType2"}, {"payCode": "PType3"}] }, { "categoryName": "Payment Type 3", "paymentOptions": [{ "payCode": "PType4"}] } ] }

const result = obj.PaymentTypes.flatMap(({ paymentOptions }) => { 
    return paymentOptions.map(({ payCode }) => payCode)
});

console.log('Result:', result)

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