Categories
JavaScript Answers

How to use the reduce function to return an array with JavaScript?

Sometimes, we want to use the reduce function to return an array with JavaScript.

In this article, we’ll look at how to use the reduce function to return an array with JavaScript.

How to use the reduce function to return an array with JavaScript?

To the use reduce function to return an array with JavaScript, we use the map method.

For instance, we write

const store = [0, 1, 2, 3, 4];

const stored = store.map((pV) => {
  return pV;
});

to call store.map with a function that returns pV, which is the value of the store being looped through.

Conclusion

To the use reduce function to return an array with JavaScript, we use the map method.

Categories
JavaScript Answers

How to use setTimeout synchronously in JavaScript?

Sometimes, we want to use setTimeout synchronously in JavaScript.

In this article, we’ll look at how to use setTimeout synchronously in JavaScript.

How to use setTimeout synchronously in JavaScript?

To use setTimeout synchronously in JavaScript, we wrap setTimeout in a promise.

For instance, we write

const sleep = (ms) => {
  return new Promise((resolve) => setTimeout(resolve, ms));
};

const demo = async () => {
  console.log("this should be the first one");
  await sleep(5000);
  console.log("this should be the second one");
};

demo();

to define the sleep function that returns a promise that calls setTimeout which calls resolve after ms milliseconds.

Then we define the demo function that calls console.log, sleep and console.log again.

We should see the first line logged, and then the 2nd line logged 5 seconds after.

Conclusion

To use setTimeout synchronously in JavaScript, we wrap setTimeout in a promise.

Categories
JavaScript Answers

How to stop HTML5 video playback on modal window close with JavaScript?

Sometimes, we want to stop HTML5 video playback on modal window close with JavaScript.

In this article, we’ll look at how to stop HTML5 video playback on modal window close with JavaScript.

How to stop HTML5 video playback on modal window close with JavaScript?

To stop HTML5 video playback on modal window close with JavaScript, we call the pause method.

For instance, we write

const video = document.getElementById("myVideoPlayer");
const stopVideo = () => {
  video.pause();
  video.currentTime = 0;
};

to select the video element with getElementById.

Then in the stopVideo function, we call video.pause to pause the video.

And we set its currentTime to 0 seconds to move the video back to the beginning.

Conclusion

To stop HTML5 video playback on modal window close with JavaScript, we call the pause method.

Categories
JavaScript Answers

How to split a comma separated string and process in a loop using JavaScript?

Sometimes, we want to split a comma separated string and process in a loop using JavaScript.

In this article, we’ll look at how to split a comma separated string and process in a loop using JavaScript.

How to split a comma separated string and process in a loop using JavaScript?

To split a comma separated string and process in a loop using JavaScript, we use the split method and a for-of loop.

For instance, we write

const str = "Hello, World, etc";
const myArray = str.split(",");

for (const a of myArray) {
  console.log(a);
}

to call str.split with ',' to return an array with the strings between the commas.

And then we use a for-of loop to loop through the myArray entries.

We get the entry being looped through from a.

Conclusion

To split a comma separated string and process in a loop using JavaScript, we use the split method and a for-of loop.

Categories
JavaScript Answers

How to sort an array with arrays in it by string with JavaScript?

Sometimes, we want to sort an array with arrays in it by string with JavaScript.

In this article, we’ll look at how to sort an array with arrays in it by string with JavaScript.

How to sort an array with arrays in it by string with JavaScript?

To sort an array with arrays in it by string with JavaScript, we use the localeCompare method.

For instance, we write

const sorted = myArray.sort((a, b) => a.localeCompare(b));

to call myArray.sort with a callback that gets the values a and b being looped through from the parameters.

Then we compare them with a.localeCompare(b) to return a sorted array that sorts the strings in ascending order.

Conclusion

To sort an array with arrays in it by string with JavaScript, we use the localeCompare method.