4. Thêm giá trị vào đầu mảng trong Chart.js

Array unshift method for Chart js

Understanding the array unshift method in javascript will give you a great benefit for Chart.js. With the array unshift method you can start adding values at the beginning of an array.

Doing this with Chart.js means seeing your chart being updated as values are moved. The previous first value moves to second in position and your indicated new value will be the first in your array.

Array Unshift Chart.js

Array Unshift Chart.js Code

By using the array unshift method you add another value into your array. The unshift method will add a new value to the beginning of the existing array. Which is than considered the new first value within the array. See code sample below for basic understanding.

const colors = ['red', 'blue', 'green'];

colors.unshift('yellow') // colors = ['yellow', 'red', 'blue', 'green'];

The four basic array methods

If you start developing with Chart.js there are four specific array methods you should begin with. There are many more that are much more powerful but tend to be more complicated. So having a foundation before explore more advance array methods.

  1. Array Push Method
  2. Array Pop Method
  3. Array Unshift Method
  4. Array Shift Method

Once you understand the basics you can continue on with the ore advance which gives you much more options such as the array method as array indexOf, array slice, and more which are more deeper into the Array Series. Or combining any of these together.

Trang này có hữu ích không?

Trả lời