Categories
JavaScript Answers

How to create line breaks in console.log() in Node.js and JavaScript?

Sometimes, we want to create line breaks in console.log() in Node.js and JavaScript.

In this article, we’ll look at how to create line breaks in console.log() in Node.js and JavaScript.

How to create line breaks in console.log() in Node.js and JavaScript?

To create line breaks in console.log() in Node.js and JavaScript, we log the '\n' character.

For instance, we write

console.log({ a: 1 }, "\n", { b: 3 }, "\n", { c: 3 });

to call console.log with '\n' separating each object to log each object in their own line.

Conclusion

To create line breaks in console.log() in Node.js and JavaScript, we log the '\n' character.

Categories
JavaScript Answers

How to show a spinner while loading an image via JavaScript?

Sometimes, we want to show a spinner while loading an image via JavaScript.

In this article, we’ll look at how to show a spinner while loading an image via JavaScript.

How to show a spinner while loading an image via JavaScript?

To show a spinner while loading an image via JavaScript, we add the spinner image and show it.

For instance, we write

<div><img src="spinner.gif" id="spinnerImg" style="display: none" /></div>

to add the spinner image and hide it by default.

Then we write

const hideSpinner = () => {
  document.getElementById("spinnerImg").style.display = "none";
};

const chartOnClick = () => {
  const spinnerShowTime = 3000;
  document.getElementById("spinnerImg").style.display = "";
  document.getElementById("chart").src = "/charts/10.png";
  setTimeout(hideSpinner, spinnerShowTime);
};

to load the image we want to show in the chartOnClick function.

We show the spinner with

document.getElementById("spinnerImg").style.display = "";

Then we load the image with

document.getElementById("chart").src = "/charts/10.png";

Then we the spinner in the hideSpinner function with

document.getElementById("spinnerImg").style.display = "none";

Conclusion

To show a spinner while loading an image via JavaScript, we add the spinner image and show it.

Categories
JavaScript Answers

How to get the size of JSON object with JavaScript?

Sometimes, we want to get the size of JSON object with JavaScript.

In this article, we’ll look at how to get the size of JSON object with JavaScript.

How to get the size of JSON object with JavaScript?

To get the size of JSON object with JavaScript, we use the TextEncode constructor.

For instance, we write

const size = new TextEncoder().encode(JSON.stringify(obj)).length;
const kiloBytes = size / 1024;
const megaBytes = kiloBytes / 1024;

to create a TextEncoder object and call its encode method with the JSON string we want to get the size of.

Then we get the size of that with length in bytes.

Next, we get the size in KB and MB by dividing each by 1024.

Conclusion

To get the size of JSON object with JavaScript, we use the TextEncode constructor.

Categories
JavaScript Answers

How to fix “forEach not a function” error for JavaScript objects?

Sometimes, we want to fix "forEach not a function" error for JavaScript objects.

In this article, we’ll look at how to fix "forEach not a function" error for JavaScript objects.

How to fix "forEach not a function" error for JavaScript objects?

To fix "forEach not a function" error for JavaScript objects, we can use some object methods.

For instance, we write

Object.values(a).forEach((value) => {
  console.log(value);
});

to call Object.values with a to return all the property value of object a in an array.

Then we call forEach with a callback to log each value.

Conclusion

To fix "forEach not a function" error for JavaScript objects, we can use some object methods.

Categories
JavaScript Answers

How to display multiple Google Maps per page with API V3 and JavaScript?

Sometimes, we want to display multiple Google Maps per page with API V3 and JavaScript.

In this article, we’ll look at how to display multiple Google Maps per page with API V3 and JavaScript.

How to display multiple Google Maps per page with API V3 and JavaScript?

To display multiple Google Maps per page with API V3 and JavaScript, we load each map on a different element.

For instance, we write

<div
  id="map_canvas"
  style="width: 700px; height: 500px; margin-left: 80px"
></div>
<div
  id="map_canvas2"
  style="width: 700px; height: 500px; margin-left: 80px"
></div>

to add 2 divs for 2 maps.

Then we write

let map;
let map2;

const initialize = (condition) => {
  const myOptions = {
    zoom: 14,
    center: new google.maps.LatLng(0.0, 0.0),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
  };
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  map2 = new google.maps.Map(document.getElementById("map_canvas2"), myOptions);
};

to call the Map constructor with each div and myOptions to load the maps in the initialize function.

Conclusion

To display multiple Google Maps per page with API V3 and JavaScript, we load each map on a different element.