Categories
JavaScript Answers

How to close all info windows in Google Maps API v3 and JavaScript?

Sometimes, we want to close all info windows in Google Maps API v3 and JavaScript.

In this article, we’ll look at how to close all info windows in Google Maps API v3 and JavaScript.

How to close all info windows in Google Maps API v3 and JavaScript?

To close all info windows in Google Maps API v3 and JavaScript, we can call the close method.

For instance, we write

const latlng = new google.maps.LatLng(-34.397, 150.644);
let infoWindow = null;

//..

google.maps.event.addListener(marker, "click", () => {
  infoWindow?.close();
  infoWindow = new google.maps.InfoWindow();
  //...
});

to call infoWindow?.close() to close the info window when we click on a marker.

Then we create a new info window with

infoWindow = new google.maps.InfoWindow();

Conclusion

To close all info windows in Google Maps API v3 and JavaScript, we can call the close method.

Categories
JavaScript Answers

How to read Excel file using Node.js and JavaScript?

Sometimes, we want to read Excel file using Node.js and JavaScript

In this article, we’ll look at how to read Excel file using Node.js and JavaScript.

How to read Excel file using Node.js and JavaScript?

To read Excel file using Node.js and JavaScript, we use the node-xlsx package.

To install it, we run

npm install node-xlsx --save

Then we use it by writing

const xlsx = require("node-xlsx");

const obj = xlsx.parse(__dirname + "/myFile.xlsx");

to parse a file at the path with xlsx.parse.

We can parse a buffer with

const obj = xlsx.parse(fs.readFileSync(__dirname + "/myFile.xlsx"));

We read the file with readFileSync and then parse the returned buffer.

Conclusion

To read Excel file using Node.js and JavaScript, we use the node-xlsx package.

Categories
JavaScript Answers

How to make synchronous requests in Node.js and JavaScript?

Sometimes, we want to make synchronous requests in Node.js and JavaScript.

In this article, we’ll look at how to make synchronous requests in Node.js and JavaScript.

How to make synchronous requests in Node.js and JavaScript?

To make synchronous requests in Node.js and JavaScript, we can use the retus library.

To install it, we run

npm i retus

Then we use it by writing

const retus = require("retus");

const { body } = retus("https://example.com");

to make a get request to https://example.com synchronously.

body has the response body.

Conclusion

To make synchronous requests in Node.js and JavaScript, we can use the retus library.

Categories
JavaScript Answers

How to prettify JSON data in text area input with JavaScript?

Sometimes, we want to prettify JSON data in text area input with JavaScript.

In this article, we’ll look at how to prettify JSON data in text area input with JavaScript.

How to prettify JSON data in text area input with JavaScript?

To prettify JSON data in text area input with JavaScript, we use the JSON.stringify method.

For instance, we write

<textarea id="myTextArea" cols="50" rows="10"></textarea>
<button>Pretty Print</button>

to add a text area and a button.

Then we write

const button = document.querySelector("button");

button.onclick = () => {
  const ugly = document.getElementById("myTextArea").value;
  const obj = JSON.parse(ugly);
  const pretty = JSON.stringify(obj, undefined, 4);
  document.getElementById("myTextArea").value = pretty;
};

to select the button with querySelector.

And then we set button.onclick to a function that formats the JSON by calling JSON.parse to parse the ugly JSON string.

Next we call JSON.stringify with obj and 4 to format the string with 4 spaces for indentation.

And then we assign the formatted pretty JSON string back to the text area.

Conclusion

To prettify JSON data in text area input with JavaScript, we use the JSON.stringify method.

Categories
JavaScript Answers

How to round up or down a moment.js to the nearest minute with JavaScript?

Sometimes, we want to round up or down a moment.js to the nearest minute with JavaScript.

In this article, we’ll look at how to round up or down a moment.js to the nearest minute with JavaScript.

How to round up or down a moment.js to the nearest minute with JavaScript?

To round up or down a moment.js to the nearest minute with JavaScript, we use the startOf method.

For instance, we write

const roundDown = moment("2022-02-17 12:59:59").startOf("hour");
roundDown.format("HH:mm:SS");

to round up to the nearest hour by calling startOf.

Then we write

const roundUp = (momentObj, roundBy) => {
  return momentObj.add(1, roundBy).startOf(roundBy);
};

const a = moment("2022-02-17 12:00:00");
const roundedUp = roundUp(a, "minute").format("HH:mm:SS");

to define the roundUp function.

In it, we call add with 1 and roundBy to add 1 roundBy unit to the moment object.

Then we call startOf with roundBy to round to the nearest roundBy unit.

Next we call roundUp and format to round up and format the time returned.

Conclusion

To round up or down a moment.js to the nearest minute with JavaScript, we use the roundTo method.