简体   繁体   English

如何从 JavaScript 中的对象内部访问属于同一对象的属性?

[英]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).假设我有一个这样的对象(它是一个图表 js 设置)。


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?首先,以这种方式访问​​它不是一个好选择?

You can access the properties of an object in JavaScript in 3 ways: 1.Dot property accessor: object.在 JavaScript 中可以通过 3 种方式访问​​对象的属性: 1.Dot 属性访问器:对象。 property.财产。 2.Square brackets property access: object['property'] 3.Object destructuring: const { property } = objec 2.方括号属性访问:object['property'] 3.Object解构:const { property } = objec

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问 JavaScript 中符号类型 object 内的属性? - How can I access properties inside a symbol type object in JavaScript? 如何从Javascript中相同对象的方法内部访问对象的值? - How to access the value of an object from inside a method of the same object in Javascript? JS:如何访问另一个函数内部的函数中的变量值,并且两个函数都属于同一对象? - JS: How may I access a variable value inside a function that's inside another function, and that both functions belong to the same object? 如何从另一个模块JS对象中的JavaScript对象访问函数和变量? - How can I access functions and variables from my JavaScript object inside of another module JS object? 在JavaScript中,如何访问对象数组内的对象属性? - In JavaScript, how to access object properties inside an array of object? 如何从Javascript对象访问属性? - How to access properties from a Javascript object? 如何从数组中的对象访问属性? - How can I access properties from an object within an array? 如何访问对象的属性 - how can i access properties of an object 如何访问另一个对象内的对象属性 - how to access object properties inside another object 如何在javascript中访问对象属性? - How to access object properties in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM