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

76
Vistas
loop through a dynamic key of an object in a array

I have 2 arrays. The one contains the name of all the keys of the 2nd array like the following.

    const additionalFields = ['phoneNumbers', 'ageOrDobs', 'genders', 'usernames', 'employments', 'educations', 'websites', 'affiliatedOrganizations'];

const fields= [{
    affiliatedOrganizations: []
    ageOrDobs: []
    educations: []
    emails: [{…}]
    employments: []
    genders: []
    locations: [{…}]
    names: [{…}]
    phoneNumbers: []
    usernames: []
    websites: []
}]

Now, I want to loop through the additionalFields based on the value I receive I want to check if the key of fields have length greater than 0. For example.. fields.phonenNumbers.length > 0 but the key phoneNumbers will be coming from loop

additionalFields.map((opts) => {
     if(fields.opts.length > 0){ //this one doesn't work...
        //do something
     }
}

I am using typescript and fields have a interface related to the values in it as shown above. Please help me with this... thank you!!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Just use the Bracket notation:

additionalFields.map((opts) => {
  if(fields[opts].length > 0){ //this one doesn't work...
    //do something
  }
}

Example:

const person = {
  firstname: 'John',
  lastname: 'Doe'
}

const key = "firstname"
console.log(person[key]) // John

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