Categories
JavaScript Answers

How to get the element with the highest occurrence in an array with JavaScript?

Sometimes, we want to get the element with the highest occurrence in an array with JavaScript.

In this article, we’ll look at how to get the element with the highest occurrence in an array with JavaScript.

How to get the element with the highest occurrence in an array with JavaScript?

To get the element with the highest occurrence in an array with JavaScript, we can use some array methods.

For instance, we write

const mode = (arr) => {
  return arr
    .sort(
      (a, b) =>
        arr.filter((v) => v === a).length - arr.filter((v) => v === b).length
    )
    .pop();
};

const highest = mode(["pear", "apple", "orange", "apple"]);

to define the mode function that returns arr sorted by the most occurrences of each entry.

We call arr.sort with a callback that get the length of the array with value v equal to a and b.

We use filter to get an array with all the entries with a and b.

Then we subtract their lengths to do the sorting in ascending order.

Finally, we call pop to return the last entry after removing it.

Therefore, highest is 'apple'.

Conclusion

To get the element with the highest occurrence in an array with JavaScript, we can use some array methods.

Categories
JavaScript Answers

How to resize a Google Map with JavaScript after it has loaded?

Sometimes, we want to resize a Google Map with JavaScript after it has loaded.

In this article, we’ll look at how to resize a Google Map with JavaScript after it has loaded.

How to resize a Google Map with JavaScript after it has loaded?

To resize a Google Map with JavaScript after it has loaded, we trigger the resize event in the idle event handler.

For instance, we write

google.maps.event.addListener(map, "idle", () => {
  google.maps.event.trigger(map, "resize");
});

to call addEventListener to listen for the idle event.

In the idle event listener, we call trigger with map and 'resize' to resize the map after it has loaded.

Conclusion

To resize a Google Map with JavaScript after it has loaded, we trigger the resize event in the idle event handler.

Categories
JavaScript Answers

How to print PDF directly from JavaScript?

Sometimes, we want to print PDF directly from JavaScript.

In this article, we’ll look at how to print PDF directly from JavaScript.

How to print PDF directly from JavaScript?

To print PDF directly from JavaScript, we can call the print method.

For instance, we write

<embed
  type="application/pdf"
  src="path_to_pdf_document.pdf"
  id="pdfDocument"
  width="100%"
  height="100%"
/>

to embed the PDF with an embed element.

Then we write

const printDocument = (documentId) => {
  const doc = document.getElementById(documentId);

  if (typeof doc.print === "undefined") {
    setTimeout(() => {
      printDocument(documentId);
    }, 1000);
  } else {
    doc.print();
  }
};

to define the printDocument function.

In it, we get the embed element with getElementById.

Then we run printDocument repeatedly until the doc.print method is defined.

Once it’s defined, we call doc.print to open the print dialog.

Conclusion

To print PDF directly from JavaScript, we can call the print method.

Categories
JavaScript Answers

How to download a div in a HTML page as PDF using JavaScript?

Sometimes, we want to download a div in a HTML page as PDF using JavaScript.

In this article, we’ll look at how to download a div in a HTML page as PDF using JavaScript.

How to download a div in a HTML page as PDF using JavaScript?

To download a div in a HTML page as PDF using JavaScript, we can use the jsPDF library.

To install it, we run

npm install jspdf --save

Then we use it by writing

import { jsPDF } from "jspdf";

const doc = new jsPDF();

doc.text("Hello world!", 10, 10);
doc.save("a4.pdf");

to create a jsPDF object.

Then we call doc.text to add the string to the object as its content.

Next, we call save with the file name of the PDF to save the PDF.

We replace the string with the HTML string of the div that we get from the textContent property.

Conclusion

To download a div in a HTML page as PDF using JavaScript, we can use the jsPDF library.

Categories
JavaScript Answers

How to get value in an object’s key using a variable referencing that key with JavaScript?

Sometimes, we want to get value in an object’s key using a variable referencing that key with JavaScript.

In this article, we’ll look at how to get value in an object’s key using a variable referencing that key with JavaScript.

How to get value in an object’s key using a variable referencing that key with JavaScript?

To get value in an object’s key using a variable referencing that key with JavaScript, we can use square brackets.

For instance, we write

console.log(obj[name]);

to get the name property of the obj object with obj[name].

name is a string with the key name of the property in obj we want to get.

Conclusion

To get value in an object’s key using a variable referencing that key with JavaScript, we can use square brackets.