Categories
JavaScript Answers

How to convert Map to JSON object in JavaScript?

Sometimes, we want to convert Map to JSON object in JavaScript.

In this article, we’ll look at how to convert Map to JSON object in JavaScript.

How to convert Map to JSON object in JavaScript?

To convert Map to JSON object in JavaScript, we use the Object.fromEntries method.

For instance, we write

const map1 = new Map([
  ["foo", "qux"],
  ["baz", 100],
]);

const obj = Object.fromEntries(map1);

to create map1 with the Map constructor.

Then we call Object.fromEntries to return an array of key-value pair arrays from map1.

To convert the obj object back to a map, we write

const map2 = new Map(Object.entries(obj));
Categories
JavaScript Answers

How to get max and min value from array in JavaScript?

Sometimes, we want to get max and min value from array in JavaScript.

In this article, we’ll look at how to get max and min value from array in JavaScript.

How to get max and min value from array in JavaScript?

To get max and min value from array in JavaScript, we use some math methods.

For instance, we write

const array = [1, 3, 2];
const max = Math.max(...array);
const min = Math.min(...array);

to spread the entries of array as arguments of Math.max and Math.min.

max returns the max value in the list of arguments.

min returns the max value in the list of arguments.

Conclusion

To get max and min value from array in JavaScript, we use some math methods.

Categories
JavaScript Answers

How to set hours, minutes, seconds for Date which is in GMT with JavaScript?

Sometimes, we want to set hours, minutes, seconds for Date which is in GMT with JavaScript.

In this article, we’ll look at how to set hours, minutes, seconds for Date which is in GMT with JavaScript.

How to set hours, minutes, seconds for Date which is in GMT with JavaScript?

To set hours, minutes, seconds for Date which is in GMT with JavaScript, we use some date methods.

For instance, we write

const today = new Date();

const myToday = new Date(
  today.getFullYear(),
  today.getMonth(),
  today.getDate(),
  0,
  0,
  0
);

to create the today date object.

Then we create the myToday object that sets the hour, minute, and second to 0 and the rest of the values comes from today.

getFullYear returns the 4 digit year.

getMonth returns the month from 0 for January and 11 for December.

getDate returns the date of the month.

Conclusion

To set hours, minutes, seconds for Date which is in GMT with JavaScript, we use some date methods.

Categories
JavaScript Answers

How to hide a mobile browser’s address bar with JavaScript?

Sometimes, we want to hide a mobile browser’s address bar with JavaScript.

In this article, we’ll look at how to hide a mobile browser’s address bar with JavaScript.

How to hide a mobile browser’s address bar with JavaScript?

To hide a mobile browser’s address bar with JavaScript, we scroll down the page slightly.

For instance, we write

window.addEventListener("load", () => {
  setTimeout(() => {
    window.scrollTo(0, 1);
  }, 0);
});

to scroll down the page when it’s loaded.

We listen to the load event with addEventListener.

Then we call window.scrollTo to scroll down the page by 1 pixel in the setTimeout callback when the load event is triggered when the page is loaded to hide the address bar.

Conclusion

To hide a mobile browser’s address bar with JavaScript, we scroll down the page slightly.

Categories
JavaScript Answers

How to show or hide controls programmatically in a HTML5 video with JavaScript?

Sometimes, we want to show or hide controls programmatically in a HTML5 video with JavaScript.

In this article, we’ll look at how to show or hide controls programmatically in a HTML5 video with JavaScript.

How to show or hide controls programmatically in a HTML5 video with JavaScript?

To show or hide controls programmatically in a HTML5 video with JavaScript, we can add or remove the controls attribute programmatically.

For instance, we write

<video id="myvideo">
  <source src="path/to/movie.mp4" />
</video>

<p onclick="toggleControls();">Toggle</p>

to add the video and p element.

Then we write

const video = document.getElementById("myvideo");

function toggleControls() {
  if (video.hasAttribute("controls")) {
    video.removeAttribute("controls");
  } else {
    video.setAttribute("controls", "controls");
  }
}

to select the video element with getElementById.

When we click on the p element, toggleControls is called.

In it, we check if the controls attribute is added to the video element with hasAttribute.

If it is, we call removeAttribute to remove it.

Otherwise, we call setAttribute to add it.

Conclusion

To show or hide controls programmatically in a HTML5 video with JavaScript, we can add or remove the controls attribute programmatically.