-1

There is some javascript code below. Please, can you tell me how to access and change the variable data? (I mean add some numbers to array)

    const data = {
      labels: labels,
      datasets: [{
        label: 'myDataLabel',
        backgroundColor: 'green',
        borderColor: 'green',
        data: [0, 6, 4],
      }]
    };

I tried console.log(data.datasets.data), but console show that it's undefined

1 Answers1

0

data.datasets is an array, so you need to access the first (and only) element using [0]:

data.datasets[0].data.push(10, 20);

That will push 10 and 20 onto the end of the data array. Using . to access data would work if datasets was an object with a data property, but it's not, it's an array. So you have to use array notation ([...]) to access its elements.

HKTE
  • 167
  • 5