Categories
JavaScript Answers

How to print part of a rendered HTML page in JavaScript?

Sometimes, we want to print part of a rendered HTML page in JavaScript.

In this article, we’ll look at how to print part of a rendered HTML page in JavaScript.

How to print part of a rendered HTML page in JavaScript?

To print part of a rendered HTML page in JavaScript, we can open a new window with the element we want to print.

For instance, we write

const newWindow = window.open();
newWindow.document.write(document.getElementById("output").innerHTML);
newWindow.print();

to call window.open to open a new window.

Then we call newWindow.document.write to render the HTML string of the element we want to print.

We get the element with getElementById and get the string with the HTML code for the element with innerHTML.

Next we call print to open the print dialog to print the stuff in the new window.

Conclusion

To print part of a rendered HTML page in JavaScript, we can open a new window with the element we want to print.

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.