Categories
JavaScript Answers

How to sort an array of objects with JavaScript?

Sometimes, we want to sort an array of objects with JavaScript.

In this article, we’ll look at how to sort an array of objects with JavaScript.

How to sort an array of objects with JavaScript?

To sort an array of objects with JavaScript, we call the array sort method.

For instance, we write

const library = [
  { name: "Steve", course: "WAP", courseID: "cs452" },
  { name: "Rakesh", course: "WAA", courseID: "cs545" },
  { name: "Asad", course: "SWE", courseID: "cs542" },
];

const sortedByNname = library.sort((a, b) => a.name > b.name);

to call library.sort with a function that compares the name values of each entry alphabetically.

It returns an array sorted by the name property value of each object.

Conclusion

To sort an array of objects with JavaScript, we call the array sort method.

Categories
JavaScript Answers

How to write regex for allowing alphanumeric, – , _ and space with JavaScript?

Sometimes, we want to write regex for allowing alphanumeric, – , _ and space with JavaScript.

In this article, we’ll look at how to write regex for allowing alphanumeric,- , _ and space with JavaScript.

How to write regex for allowing alphanumeric, – , _ and space with JavaScript?

To write regex for allowing alphanumeric, – , _ and space with JavaScript, we use various patterns.

For instance, we write

const re = /^[\w\-\s]+$/;

to define the regex to match alphanumeric, – , _ and space.

\w matches alphanumeric characters and _.

\- matches -,

\s matches spaces.

^ indicates the start of a word.

$ indicates the end of a word.

Conclusion

To write regex for allowing alphanumeric, – , _ and space with JavaScript, we use various patterns.

Categories
JavaScript Answers

How to fix document.getElementByID is not a function error with JavaScript?

Sometimes, we want to fix document.getElementByID is not a function error with JavaScript.

In this article, we’ll look at how to fix document.getElementByID is not a function error with JavaScript.

How to fix document.getElementByID is not a function error with JavaScript?

To fix document.getElementByID is not a function error with JavaScript, we call getElementById instead of getElementByID.

For instance, we write

const element = document.getElementById("foo");

to call getElementById to get the element with ID foo.

Conclusion

To fix document.getElementByID is not a function error with JavaScript, we call getElementById instead of getElementByID.

Categories
JavaScript Answers

How to retrieve HTML5 video duration separately from the file with JavaScript?

Sometimes, we want to retrieve HTML5 video duration separately from the file with JavaScript.

In this article, we’ll look at how to retrieve HTML5 video duration separately from the file with JavaScript.

How to retrieve HTML5 video duration separately from the file with JavaScript?

To retrieve HTML5 video duration separately from the file with JavaScript, we listen for the durationchange event.

For instance, we write

const myVideoPlayer = document.getElementById("video_player");

myVideoPlayer.addEventListener("durationchange", () => {
  console.log("Duration change", myVideoPlayer.duration);
});

to select the video element with getElementById.

Then we listen for the durationchange event with addEventListener.

And we get the duration of the video with the duration property.

Conclusion

To retrieve HTML5 video duration separately from the file with JavaScript, we listen for the durationchange event.

Categories
JavaScript Answers

How to convert a 2D JavaScript array to a 1D array?

Sometimes, we want to convert a 2D JavaScript array to a 1D array.

In this article, we’ll look at how to convert a 2D JavaScript array to a 1D array.

How to convert a 2D JavaScript array to a 1D array?

To convert a 2D JavaScript array to a 1D array, we use the array flat method.

For instance, we write

const arrToConvert = [
  [0, 0, 1],
  [2, 3, 3],
  [4, 4, 5],
];
const newArr = arrToConvert.flat();

to call arrToConvert.flat to return a flattened version of the arrToConvert array.

Conclusion

To convert a 2D JavaScript array to a 1D array, we use the array flat method.