Categories
JavaScript Answers

How to create array of unique objects by property with JavaScript?

To create array of unique objects by property with JavaScript, we use the array filter method.

For instance, we write

const getUniqueBy = (arr, prop) => {
  const set = new Set();
  return arr.filter((o) => !set.has(o[prop]) && set.add(o[prop]));
};

to define the getUniqueBy function.

In it, we create a set with the Set constructor.

And then we return the array returned by filter that adds the o[prop] to the set if it doesn’t exist and if the property value doesn’t exist.

Categories
JavaScript Answers

How to force the soft keyboard to hide with JavaScript?

Sometimes, we want to force the soft keyboard to hide with JavaScript.

In this article, we’ll look at how to force the soft keyboard to hide with JavaScript.

How to force the soft keyboard to hide with JavaScript?

To force the soft keyboard to hide with JavaScript, we call blur when we focus on the input.

For instance, we write

<input type="text" id="phone-number" onfocus="blur();" />;

to call blur when we focus on the input by setting the onfocus attribute to blur().

Conclusion

To force the soft keyboard to hide with JavaScript, we call blur when we focus on the input.

Categories
JavaScript Answers

How to open new page in same window with JavaScript?

Sometimes, we want to open new page in same window with JavaScript.

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

How to open new page in same window with JavaScript?

To open new page in same window with JavaScript, we call the window.open method.

For instance, we write

window.open("http://example.com", "_self", false);

to call window.open with the URL we want to open and '_self' to open the new page in the same window.

Conclusion

To open new page in same window with JavaScript, we call the window.open method.

Categories
JavaScript Answers

How to go to URL after OK button if alert is pressed with JavaScript?

Sometimes, we want to go to URL after OK button if alert is pressed with JavaScript.

In this article, we’ll look at how to go to URL after OK button if alert is pressed with JavaScript.

How to go to URL after OK button if alert is pressed with JavaScript?

To go to URL after OK button if alert is pressed with JavaScript, we use the confirm function.

For instance, we write

if (confirm("are you sure?")) {
  document.location = "http://example.com/";
}

to call confirm with the message string to display.

If we click OK, then true is returned and the code in the if block is run.

Conclusion

To go to URL after OK button if alert is pressed with JavaScript, we use the confirm function.

Categories
JavaScript Answers

How to mute an HTML video player using JavaScript?

Sometimes, we want to mute an HTML video player using JavaScript.

In this article, we’ll look at how to mute an HTML video player using JavaScript.

How to mute an HTML video player using JavaScript?

To mute an HTML video player using JavaScript, we set the muted property.

For instance, we write

const video = document.getElementById("your-video-id");
video.muted = true;

to select the video element with getElementById.

We then set muted to true to mute the video.

We write

video.muted = false;

to unmute the video.

Conclusion

To mute an HTML video player using JavaScript, we set the muted property.