Categories
JavaScript Answers

How to limit labels number on Chart.js line chart with JavaScript?

Sometimes, we want to limit labels number on Chart.js line chart with JavaScript.

In this article, we’ll look at how to limit labels number on Chart.js line chart with JavaScript.

How to limit labels number on Chart.js line chart with JavaScript?

To limit labels number on Chart.js line chart with JavaScript, we can add the maxTicksLimit property.

For instance, we write

const options = {
  scales: {
    x: {
      ticks: {
        maxTicksLimit: 10,
      },
    },
  },
};

const myLineChart = new Chart(ctx, {
  type: "line",
  data,
  options,
});

to create a Chart object with the canvas context ctx and an object with the options property.

We set the options.scales.x.ticks.maxTicksLimit property to 10 to limit the number of x-axis ticks to 10.

Conclusion

To limit labels number on Chart.js line chart with JavaScript, we can add the maxTicksLimit property.

Categories
JavaScript Answers

How to convert JSON object to JavaScript array?

Sometimes, we want to convert JSON object to JavaScript array.

In this article, we’ll look at how to convert JSON object to JavaScript array.

How to convert JSON object to JavaScript array?

To convert JSON object to JavaScript array, we can use the Object.entries method.

For instance, we write

const jsonData = { "2022-01-21": 1, "2022-01-22": 7 };

console.log(Object.entries(jsonData));

to call Object.entries with the jsonData object to return an array with the key-value pairs of the jsonData object as arrays.

Conclusion

To convert JSON object to JavaScript array, we can use the Object.entries method.

Categories
JavaScript Answers

How to add a year to today’s date with JavaScript?

Sometimes, we want to add a year to today’s date with JavaScript.

In this article, we’ll look at how to add a year to today’s date with JavaScript.

How to add a year To today’s date with JavaScript?

To add a year to today’s date with JavaScript, we can use the date’s getFullYear and setFullYear methods.

For instance, we write

const aYearFromNow = new Date();
aYearFromNow.setFullYear(aYearFromNow.getFullYear() + 1);
console.log(aYearFromNow);

to create the aYearFromNow Date object with the current date and time.

Then we call aYearFromNow.getFullYear to get the 4 digit year number of the date.

Then we add 1 to it and use the sum as the argument for setFullYear to increment the year of aYearFromNow by 1.

Conclusion

To add a year to today’s date with JavaScript, we can use the date’s getFullYear and setFullYear methods.

Categories
JavaScript Answers

How to sort 2 dimensional array by column value with JavaScript?

Sometimes, we want to sort 2 dimensional array by column value with JavaScript.

In this article, we’ll look at how to sort 2 dimensional array by column value with JavaScript.

How to sort 2 dimensional array by column value with JavaScript?

To sort 2 dimensional array by column value with JavaScript, we can use the array sort method.

For instance, we write

const arr = [
  [12, "AAA"],
  [12, "BBB"],
  [12, "CCC"],
  [28, "DDD"],
  [18, "CCC"],
  [12, "DDD"],
  [18, "CCC"],
  [28, "DDD"],
  [28, "DDD"],
  [58, "BBB"],
  [68, "BBB"],
  [78, "BBB"],
];

const sortedArr = arr.sort((a, b) => {
  return a[0] - b[0];
});

to call arr.sort with a callback that sorts each array in arr by the value of the first entry in each array in ascending order.

a and b are entries in arr.

Conclusion

To sort 2 dimensional array by column value with JavaScript, we can use the array sort method.

Categories
JavaScript Answers

How to remove non-ASCII character in string with JavaScript?

Sometimes, we want to remove non-ASCII character in string with JavaScript.

In this article, we’ll look at how to remove non-ASCII character in string with JavaScript.

How to remove non-ASCII character in string with JavaScript?

To remove non-ASCII character in string with JavaScript, we use the string’s replace method.

For instance, we write

const newStr = str.replace(/[^\x00-\x7F]/g, "");

to call str.replace with the /[^\x00-\x7F]/g regex to replace all characters that don’t have character code between 0 and 127 with empty strings.

We use the g flag to find all matches of the regex pattern in str.

Conclusion

To remove non-ASCII character in string with JavaScript, we use the string’s replace method.