Categories
JavaScript Answers

How to add or subtract dates with JavaScript?

Sometimes, we want to add or subtract dates with JavaScript.

In this article, we’ll look at how to add or subtract dates with JavaScript.

How to add or subtract dates with JavaScript?

To add or subtract dates with JavaScript, we can add or subtract them directly.

For instance, we write

const date1 = new Date(2022, 02, 18);
const date2 = new Date(2022, 02, 20);

const msDiff = date2 - date1;

to create a Date objects date1 and date2.

And then we subtract date2 by date1 to get the number of milliseconds difference between them.

date1 and date2 are automatically converted to timestamps before we do the subtraction.

Conclusion

To add or subtract dates with JavaScript, we can add or subtract them directly.

Categories
JavaScript Answers

How to merge two JSON or JavaScript arrays in to one array?

Sometimes, we want to merge two JSON or JavaScript arrays in to one array.

In this article, we’ll look at how to merge two JSON or JavaScript arrays in to one array.

How to merge two JSON or JavaScript arrays in to one array?

To merge two JSON or JavaScript arrays in to one array, we use the concat method.

For instance, we write

const finalArr = json1.concat(json2);

to call json1.concat with the json2 array to return a new array with the entries of the json1 and json2 entries.

Conclusion

To merge two JSON or JavaScript arrays in to one array, we use the concat method.

Categories
JavaScript Answers

How to loop through a JavaScript object array?

Sometimes, we want to loop through a JavaScript object array.

In this article, we’ll look at how to loop through a JavaScript object array.

How to loop through a JavaScript object array?

To loop through a JavaScript object array, we use the for-of loop.

For instance, we write

const messages = [
  {
    msgFrom: "13223821242",
    msgBody: "Hi there",
  },
  {
    msgFrom: "Bill",
    msgBody: "Hello!",
  },
];

for (const message of messages) {
  console.log(message);
}

to loop through the messages array with a for-of loop.

We log the value of the messages entry message in the loop body.

Conclusion

To loop through a JavaScript object array, we use the for-of loop.

Categories
JavaScript Answers

How to get the latest and oldest record in Mongoose.js and JavaScript?

Sometimes, we want to get the latest and oldest record in Mongoose.js and JavaScript.

In this article, we’ll look at how to get the latest and oldest record in Mongoose.js and JavaScript.

How to get the latest and oldest record in Mongoose.js and JavaScript?

To get the latest and oldest record in Mongoose.js and JavaScript, we use the find and sort methods.

For instance, we write

const latest = await Model.find().sort({ $natural: -1 }).limit(1);
const oldest = await Model.find().sort({ $natural: 1 }).limit(1);

to get the latest item by calling sort with { $natural: -1 } to sort items by descending chronological order.

And we call limit with 1 to get the first returned.

And we get the oldest item by calling sort with { $natural: 1 } to sort items by ascending chronological order.

And we call limit with 1 to get the first returned.

Conclusion

To get the latest and oldest record in Mongoose.js and JavaScript, we use the find and sort methods.

Categories
JavaScript Answers

How to fix Charts.js graph not scaling to canvas size with JavaScript?

Sometimes, we want to fix Charts.js graph not scaling to canvas size with JavaScript.

In this article, we’ll look at how to fix Charts.js graph not scaling to canvas size with JavaScript.

How to fix Charts.js graph not scaling to canvas size with JavaScript?

To fix Charts.js graph not scaling to canvas size with JavaScript, we set the responsive option to false.

For instance, we write

const ctx = document.getElementById("myChart").getContext("2d");
const myChart = new Chart(ctx, {
  type: "line",
  data: {
    labels: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    datasets: [
      {
        label: "My First dataset",
        data: [1, 2, 3, 2, 1, 2, 3, 4, 5, 4],
      },
    ],
  },
  options: {
    responsive: false,
  },
});

to set options.responsive to false to make the graph scale to the canvas size.

We get the canvas with getElementById and get its context with getContext.

Conclusion

To fix Charts.js graph not scaling to canvas size with JavaScript, we set the responsive option to false.