-1

I am really new to programming and my questions are very really not good Please forgive me if I am wasting your time Why when I use labels indirectly in DatatoBarChartJS (using a "label") it works

const DataFromDataBase  = {
  label :['January', 'February', 'March', 'April', 'May', 'June', 'July'],
  datasets : [  {datasetName:'Dataset 1', data:[1,3,5,2,5,2,6]},{datasetName:'Dataset 2', data:[4,6,5,2,5,2,6]} ],
}
const label =dataFromDataBase.label;
const color = tinycolor();


const DatatoBarChartJS  = {
  label,
  datasets : dataFromDataBase.datasets.map((dataset)=>{
        return{
          label:dataset.datasetName,
          data : dataset.data,
          backgroundColor:color.toRgbString ,
        }     
    }
  )
}

but when I use it inderectly it dosnt work?

const DataFromDataBase  = {
  label :['January', 'February', 'March', 'April', 'May', 'June', 'July'],
  datasets : [  {datasetName:'Dataset 1', data:[1,3,5,2,5,2,6]},{datasetName:'Dataset 2', data:[4,6,5,2,5,2,6]} ],
}
const label =dataFromDataBase.label;
const color = tinycolor();


const DatatoBarChartJS  = {
  DataFromDataBase.label,
  datasets : dataFromDataBase.datasets.map((dataset)=>{
        return{
          label:dataset.datasetName,
          data : dataset.data,
          backgroundColor:color.toRgbString ,
        }     
    }
  )
}

the problem is problem

  • Does this answer your question? [I dont get what I expect form the fucntion](https://stackoverflow.com/questions/71648401/i-dont-get-what-i-expect-form-the-fucntion) – jsN00b Mar 28 '22 at 17:36

1 Answers1

0

When creating an object using the curly bracket syntax, you usually need to provide the name of the fields and their associated value.

const cat = {
  cute: true,
  legCount: 4,
  sound: 'miaw'
};

Depending on what "version of Javascript" you are using, there is a shorthand syntax that you can use:

const cute = true;
const legCount = 4;
const sound = 'miaw';
const cat = {
  legCount,
  sound,
  cute
}

When doing this, the name of the field will be the name of the variable, and the value of the field will be the value of the variable.

The reason you cannot use that syntax in your case is because what you are referencing is not a variable, it's a field within an object. And it kind of makes sense, because the result would be ambiguous:

// Would the result be this?
{ DatatoBarChartJS: { label: ['January', 'February', 'March', 'April', 'May', 'June', 'July'] } }
// Or this?
{ 'DatatoBarChartJS.label': ['January', 'February', 'March', 'April', 'May', 'June', 'July'] }
// Or this?
{ label: ['January', 'February', 'March', 'April', 'May', 'June', 'July'] }

You can find more information in Mozilla's object initialization documentation