Error when referencing an object’s value in another javascript object

Asked

Viewed 82 times

1

I have the following variable with several objects inside:

const dados = {
  estudouNaFaculdadeAno: {
    metricas: {
      ano: [],
      invalido: {
        num: 0,
        palavras: []
      }
    }
  },
  estudouNaFaculdadeCurso: {
    metricas: {
      curso: {
        ads: 0,
        grh: 0,
        gpi: 0
      },
      invalido: {
        num: dados.estudouNaFaculdadeAno.metricas.invalido.num,
        palavras: dados.estudouNaFaculdadeAno.metricas.invalido.palavras
      }
    }
  }
}

When executing a console.log(dados) i get the following error code: Uncaught ReferenceError: Cannot access 'dados' before initialization at dados.js:550

Could someone help me understand why? How do I get him to reference without having this problem?

PS.: This.js data file contains only the constant dados with the values to be read by other functions that are in other files . js

2 answers

1


You are referencing the object dados inside itself. It will result in error because the variable dados not yet initialized. When trying to build the object, Javascript will try to fetch the value of dados.estudouNaFaculdadeAno.metricas.invalido.num, but this value is within the object itself that is still being built, that is, it has not yet been initialized.

You can use a getter to return these values within the object itself:

Instead of:

invalido: {
   num: dados.estudouNaFaculdadeAno.metricas.invalido.num,
   palavras: dados.estudouNaFaculdadeAno.metricas.invalido.palavras
}

Use:

get invalido(){
  return {
     num: dados.estudouNaFaculdadeAno.metricas.invalido.num,
     palavras: dados.estudouNaFaculdadeAno.metricas.invalido.palavras
  }
}

See the result:

const dados = {
  estudouNaFaculdadeAno: {
    metricas: {
      ano: [],
      invalido: {
        num: 0,
        palavras: []
      }
    }
  },
  estudouNaFaculdadeCurso: {
    metricas: {
      curso: {
        ads: 0,
        grh: 0,
        gpi: 0
      },
      get invalido(){
        return {
           num: dados.estudouNaFaculdadeAno.metricas.invalido.num,
           palavras: dados.estudouNaFaculdadeAno.metricas.invalido.palavras
        }
      }
    }
  }
}

console.log(dados.estudouNaFaculdadeCurso.metricas.invalido);

  • 1

    Oops, you gave me such a light @Sam ! Thank you so much for your help :)

0

Review the structure you are using. There are two circular references in your json:

num: dados.estudouNaFaculdadeAno.metricas.invalido.num, words: data.estudouNaFaculdadeAno.metricas.invalido.words

Enter valid JSON values, for example:

num: 1234, words: "words thrown to the wind!"

Then the value for the JSON path given.estudouNaFaculdadeAno.metricas.invalido.num will be 1234.

  • So, but what I want is just to take a die from another object. Doing it the way you said I would have fixed values in the num and palavras of the object estudouNaFaculdadeCurso

Browser other questions tagged

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