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

77
Visualizações
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 Respostas
Responde à pergunta

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 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