For object in typescript, how to get the object name?

Asked

Viewed 112 times

1

I am running a repeat structure to check which fields of my form are invalid. I need to get the name of the first field that is invalid.

I tried something like:

for(var campoObrigatorio in formulario.controls){

  if(formulario.controls[campoObrigatorio].status == "INVALID"){
    var displayCampoObrigatorio = formulario.controls[campoObrigatorio];
    console.log(displayCampoObrigatorio)
    return ;
  }

}

Apparently my logic is correct, but I’m not getting the name of the object in this line:

var displayCampoObrigatorio = formulario.controls[campoObrigatorio];

Here it takes all the gift tree of the field that is with invalid status, I would like the name of the object. Ex: height, length, etc...

inserir a descrição da imagem aqui

1 answer

1


The name of each of the keys is campoObrigatorio

const objetos = {
  a: {
    prop: 1
  },
  b: {
    prop: 2
  }
};

for (let nome in objetos) {
  console.log({
    nome,
    conteudo: objetos[nome]
  })
}

  • That’s right, I switched to console.log(fieldObrigatorio) and got the result I needed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.