Execute method only if there is specific property in the object. No testing each line with hasOwnProperty() or comparing with Undefined

Asked

Viewed 61 times

1

I am working with automated tests and in my case I have several objects that contain the settings that will be inserted in the system fields.

There I have the methods Set filling the fields with the settings in the given object, for example:

dados = {
    nome: "Diego",
    sobrenome: "Henrique",
    idade: "22"
}

preencheCliente(dados){
    fillNome(dados.nome);
    fillSobrenome(dados.sobrenome);
    fillIdade(dados.idade);
}

I would like to have objects without some property and that continue to work with the method preencheCliente(), however it will accuse a >>>error<<< when doing Age Set, example:

dados2 = {
    nome: "Diego",
    sobrenome: "Henrique"
}

preencheCliente(dados){
    fillNome(dados.nome);
    fillSobrenome(dados.sobrenome);
    >>>fillIdade(dados.idade);<<<
}

I put in the title Does not resolve with hasOwnProperty(), on account that, imagine now that I have already implemented more than 100 fields, I would have to adjust for each method fills (preencheCliente, preencheProduto, preencheEndereco, etc) and for each field do the test

if (hasOwnProperty("NOME_PROPRIEDADE_OBJ")):

preencheCliente(dados){
    if (dados.hasOwnProperty('nome'))
      fillNome(dados.nome);
    if (dados.hasOwnProperty('sobrenome'))
      fillSobrenome(dados.sobrenome);
    if (dados.hasOwnProperty('idade'))
      fillIdade(dados.idade);
}

Beyond the hasOwnProperty() there are 2 other means that I discovered:

Operator IN and comparing with Undefined. See more in: https://dmitripavlutin.com/check-if-object-has-property-javascript/

However it always falls into the massive edition of pasting a test before the lines..

Is there a solution for this, without implementing all these conditionals? Or would you have to do some other kind of approach?

  • Where does this one come from Sys.Field...? You can also use Object.entries to iterate on the keys of an object, but it is difficult to give a detailed answer without knowing what this is Sys

  • I invented it for example, from the Testcomplete framework. Sys.Fieldname would be a Comobject, where I can access properties and methods from a system window for example. I will modify the example

2 answers

2

A solution would iterate through the keys of this object if you want to run some checks on each key, example:

const dados = {
    nome: "Diego",
    sobrenome: "Henrique",
    idade: "22"
}

const keys = Object.keys(dados); // [nome, sobrenome, idade]

for (const key in keys){
  // Faça algo com a chave 
}

This way if a key does not exist in the object it will not be passed to the Keys array. You can access the value by doing dados[key], this would be a scalable solution if you need to perform the same operation for 1 or N keys inside the object without having to directly control key by key.

0

Instead of changing the calls I suggest changing the approach and making the change in the definition of methods preenche... to check if a value has been passed:

preencheIdade(idade) {
  if (!idade) { // ou idade === undefined
    return;
  }
  // sua lógica continua aqui
}

Browser other questions tagged

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