Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

103
Vistas
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 Respuestas
Responde la pregunta

0

Use the Array.map function instead.

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

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda