Categories
JavaScript Answers

How to export JavaScript data to a CSV file without server interaction?

Sometimes, we want to export JavaScript data to a CSV file without server interaction.

In this article, we’ll look at how to export JavaScript data to a CSV file without server interaction.

How to export JavaScript data to a CSV file without server interaction?

To export JavaScript data to a CSV file without server interaction, we can set a link to a base64 string with the CSV content.

For instance, we write

const csvString = csvRows.join("\r\n");
a.href = "data:application/csv;charset=utf-8," + encodeURIComponent(csvString);

to create the csvString with the CSV data.

Then we put the data in a base64 string with

"data:application/csv;charset=utf-8," + encodeURIComponent(csvString)

And we assign the value to a.href where a is an a element.

Then when we click on the link, the data will be downloaded.

Conclusion

To export JavaScript data to a CSV file without server interaction, we can set a link to a base64 string with the CSV content.

Categories
JavaScript Answers

How to open PDF string in new window with JavaScript?

Sometimes, we want to open PDF string in new window with JavaScript.

In this article, we’ll look at how to open PDF string in new window with JavaScript.

How to open PDF string in new window with JavaScript?

To open PDF string in new window with JavaScript, we can open an empty window and then render an iframe in it.

For instance, we write

const pdfWindow = window.open("");
pdfWindow.document.write(
  `<iframe width='100%' height='100%' src='data:application/pdf;base64,${encodeURI(
    yourDocumentBase64VarHere
  )}''></iframe>`
);

to call window.open with an empty string to open an empty window.

Then we call pdfWindow.document.write with a string with an iframe element in it.

And its src attribute is set to the base64 PDF URL.

Then the PDF will be rendered in the iframe.

Conclusion

To open PDF string in new window with JavaScript, we can open an empty window and then render an iframe in it.

Categories
JavaScript Answers

How to change Google Map marker color with JavaScript?

Sometimes, we want to change Google Map marker color with JavaScript.

In this article, we’ll look at how to change Google Map marker color with JavaScript.

How to change Google Map marker color with JavaScript?

To change Google Map marker color with JavaScript, we can use the setIcon method or the Marker constructor.

For instance, we write

marker.setIcon("http://maps.google.com/mapfiles/ms/icons/green-dot.png");

to call setIcon with the URL string of the green marker icon.

We can do the same thing when we create the marker with

const marker = new google.maps.Marker({
  icon: "http://maps.google.com/mapfiles/ms/icons/green-dot.png",
});

Conclusion

To change Google Map marker color with JavaScript, we can use the setIcon method or the Marker constructor.

Categories
JavaScript Answers

How to generate a 5-digit number in every case with JavaScript?

Sometimes, we want to generate a 5-digit number in every case with JavaScript.

In this article, we’ll look at how to generate a 5-digit number in every case with JavaScript.

How to generate a 5-digit number in every case with JavaScript?

To generate a 5-digit number in every case with JavaScript, we can use the Math.random method.

For instance, we write

const min = 10000;
const max = 99999;
const num = Math.floor(Math.random() * (max - min + 1)) + min;

to call use Math.random() * (max - min + 1) to generate numbers between 0 and 89999.

And then we add min to that so that num is always a 5 digit number.

Conclusion

To generate a 5-digit number in every case with JavaScript, we can use the Math.random method.

Categories
JavaScript Answers

How to add dynamic key-value pairs to a JavaScript array or hash table?

Sometimes, we want to add dynamic key-value pairs to a JavaScript array or hash table.

In this article, we’ll look at how to add dynamic key-value pairs to a JavaScript array or hash table.

How to add dynamic key-value pairs to a JavaScript array or hash table?

To add dynamic key-value pairs to a JavaScript array or hash table, we can use computed key names.

For instance, we write

const obj = {};
obj[name] = val;

to add a property with the value of name string as the property name.

We assign val to as the property’s value.

Conclusion

To add dynamic key-value pairs to a JavaScript array or hash table, we can use computed key names.