Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!