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

133
Visualizações
How to get property from array of objects by property name?

I have object which is property are arrays of objects:

   messages = {
    authentication: [
      { type: 'signinFail', message: 'Login Fail! Wrong password or email.' }
    ],
    requiredText: [
      { type: 'required', message: 'This field is required.' }
    ],
    email: [
      { type: 'required', message: 'Email is required.' },
      { type: 'email', message: 'Email has an incorrect format.' }
    ],
    confirm_password: [
      { type: 'required', message: 'Password confirmation is required.' },
      { type: 'areNotEqual', message: 'Password mismatch' }
    ],
    password: [
      { type: 'required', message: 'Password is required' },
      { type: 'minlength', message: 'Password must contain 5 or more characters.' },
      { type: 'pattern', message: 'Your password must contain at least one uppercase, one lowercase, and one number' }
    ]
  };

I need to get massage from structire above while I provide two strings, subject and type to function that i created:

 getMessage(subject, type) : string {
    return this.messages[subject][type]; 
  }

For example, if I provide:

  subject = "confirm_password"
  type = "minlength"

I expect to get this message from the getMessage function:

'Password must contain 5 or more characters.'

but I get error undefined.

Any idea how to get the desired message if I provide the subject and type?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to look for the right entry in your array by using .find(). The return the .message property of the found array element:

const messages = {
    authentication: [
      { type: 'signinFail', message: 'Login Fail! Wrong password or email.' }
    ],
    requiredText: [
      { type: 'required', message: 'This field is required.' }
    ],
    email: [
      { type: 'required', message: 'Email is required.' },
      { type: 'email', message: 'Email has an incorrect format.' }
    ],
    confirm_password: [
      { type: 'required', message: 'Password confirmation is required.' },
      { type: 'areNotEqual', message: 'Password mismatch' }
    ],
    password: [
      { type: 'required', message: 'Password is required' },
      { type: 'minlength', message: 'Password must contain 5 or more characters.' },
      { type: 'pattern', message: 'Your password must contain at least one uppercase, one lowercase, and one number' }
    ],
    getMessage: function(subject,type){return this[subject].find(el=>el.type==type).message}
  };

function getMessage(subject, type){
    return messages[subject].find(el=>el.type==type).message; 
}

console.log(getMessage("password","minlength"));          // function call
console.log(messages.getMessage("password","minlength")); // method call of messages

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