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

146
Visualizações
How can I access properties that belong to the same object from inside the object in JavaScript?

Suppose that I have an object like this (it's a chart js setting).


const radarChartData = {
  labels: [3, 4, 6, 7, 8, 8],
  data: [0, 0, 0, 0, 0, 0],
  bgArr: this.labels.map((label, i) => label === 10 ? "red" : "blue"); // An error has occurred, because this code can't access its own labels in radarChartData.
  datasets: [{.....}]
}

const radarChartConfig = {
  type: ....,
}

const radarChart = new Chart(radarChartCtx, radarChartConfig)

I tried the following fix, but all failed...

bgArr: radarChartData.labels.map((label, i) => label === 10 ? "red" : "blue");

How to access its own property? In the first place, it's not a good option to access it and this way?

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

0

You can't reference an object during initialization when using object literal syntax. You need to reference the object after it is created. Only way to reference an object during initialization is when you use a constructor function.

This example uses an anonymous function as a constructor. The new object is reference with this

const data = new (function () {
  (this.labels = [3, 4, 6, 7, 8, 8]),
    (this.data = [0, 0, 0, 0, 0, 0]),
    (this.bgArr = this.labels.map((label, i) =>
      label === 10 ? "red" : "blue"
    ));
})();

const radarChartData = {
  labels: data.labels,
  data: data.data,
  bgArr: data.labels.map((label, i) => (label === 10 ? "red" : "blue")),
};

console.log(radarChartData);

/**
 * LOGS
 * { 
 * labels: [ 3, 4, 6, 7, 8, 8 ],
 * data: [ 0, 0, 0, 0, 0, 0 ],
 * bgArr: [ 'blue', 'blue', 'blue', 'blue', 'blue', 'blue' ] 
 * }
 */

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