Categories
JavaScript Answers

How to sum two arrays in single iteration with JavaScript?

Sometimes, we want to sum two arrays in single iteration with JavaScript.

In this article, we’ll look at how to sum two arrays in single iteration with JavaScript.

How to sum two arrays in single iteration with JavaScript?

To sum two arrays in single iteration with JavaScript, we use the array map method.

For instance, we write

const array1 = [1, 2, 3, 4];
const array2 = [5, 6, 7, 8];

const sum = array1.map((num, idx) => {
  return num + array2[idx];
});

to call array1.map with a function that returns the sum of num and the number in array2 at the same index as num.

An array with the sums of each entry from each array at the given index is returned.

Conclusion

To sum two arrays in single iteration with JavaScript, we use the array map method.

Categories
JavaScript Answers

How to use promise in forEach loop of array to populate an object with JavaScript?

Sometimes, we want to use promise in forEach loop of array to populate an object with JavaScript.

In this article, we’ll look at how to use promise in forEach loop of array to populate an object with JavaScript.

How to use promise in forEach loop of array to populate an object with JavaScript?

To use promise in forEach loop of array to populate an object with JavaScript, we use the Promise.all method.

For instance, we write

const res = await Promise.all([...arr.map((i) => func())]);

to call Promise.all with an array of functions returned by arr.map.

func returns a promise which is run in parallel by Promise.all.

We get the results from the promises as an array with await.

Conclusion

To use promise in forEach loop of array to populate an object with JavaScript, we use the Promise.all method.

Categories
JavaScript Answers

How to get the selected element type with JavaScript?

Sometimes, we want to get the selected element type with JavaScript.

In this article, we’ll look at how to get the selected element type with JavaScript.

How to get the selected element type with JavaScript?

To get the selected element type with JavaScript, we use the tagName property.

For instance, we write

const elementType = document.getElementById("born").tagName;

to get the element with getElementById.

Then we use the tagName property to get a string with the element type.

Conclusion

To get the selected element type with JavaScript, we use the tagName property.

Categories
JavaScript Answers

How to append an array to form data in JavaScript?

To append an array to form data in JavaScript, we convert it into a JSON string.

For instance, we write

const tags = ["this", "is", "an", "array"];
formData.append("tags", JSON.stringify(tags));

to call append with the key and the JSON array string converted from the tags array.

Categories
JavaScript Answers

How to get a YouTube thumbnail from a YouTube iframe with JavaScript?

Sometimes, we want to get a YouTube thumbnail from a YouTube iframe with JavaScript.

In this article, we’ll look at how to get a YouTube thumbnail from a YouTube iframe with JavaScript.

How to get a YouTube thumbnail from a YouTube iframe with JavaScript?

To get a YouTube thumbnail from a YouTube iframe with JavaScript, we can create the URL with the video ID and quality setting.

For instance, we write

const getYoutubeThumbnail = (url, quality) => {
  if (url) {
    let videoId, thumbnail, result;
    result = url.match(/youtube\.com.*(\?v=|\/embed\/)(.{11})/);
    if (result) {
      videoId = result.pop();
    }
    result = url.match(/youtu.be\/(.{11})/);
    if (result) {
      videoId = result.pop();
    }

    if (videoId) {
      if (typeof quality === "undefined") {
        quality = "high";
      }

      let qualityKey = "maxresdefault";
      if (quality === "low") {
        qualityKey = "sddefault";
      } else if (quality == "medium") {
        qualityKey = "mqdefault";
      } else if (quality == "high") {
        qualityKey = "hqdefault";
      }

      const thumbnail = `http://img.youtube.com/vi/${videoId}/${qualityKey}.jpg`;
      return thumbnail;
    }
  }
  return false;
};

to define the getYoutubeThumbnail function.

In it, we get the videoId from the YouTube urlwithmatch`.

We match the patterns for YouTube URLs to get the videoId.

Then we get the qualityKey to get the image file name according to the thumbnail quality.

And then we put the image thumbnail URL together.

Conclusion

To get a YouTube thumbnail from a YouTube iframe with JavaScript, we can create the URL with the video ID and quality setting.