Categories
JavaScript Answers

How to wait for .forEach() to complete with JavaScript?

Sometimes, we want to wait for .forEach() to complete with JavaScript.

In this article, we’ll look at how to wait for .forEach() to complete with JavaScript.

How to wait for .forEach() to complete with JavaScript?

To wait for .forEach() to complete with JavaScript, we should replace forEach with a for-of loop.

For instance, we write

const myAsyncLoopFunction = async (array) => {
  const allAsyncResults = [];

  for (const item of array) {
    const asyncResult = await asyncFunction(item);
    allAsyncResults.push(asyncResult);
  }

  return allAsyncResults;
};

to define the myAsyncLoopFunction function.

In it, we use a for-of loop to loop through the array.

And we use await to wait for the promise returned by asyncFunction to finish before allAsyncResults.push is called in each iteration.

Conclusion

To wait for .forEach() to complete with JavaScript, we should replace forEach with a for-of loop.

Categories
JavaScript Answers

How to store a byte array in JavaScript?

Sometimes, we want to store a byte array in JavaScript.

In this article, we’ll look at how to store a byte array in JavaScript.

How to store a byte array in JavaScript?

To store a byte array in JavaScript, we can use the Uint8Array constructor.

For instance, we write

const array = new Uint8Array(100);
array[10] = 256;

to create the array Uint8Array with length 100.

Then we set the value at index 10 to 256.

Conclusion

To store a byte array in JavaScript, we can use the Uint8Array constructor.

Categories
JavaScript Answers

How to render a PDF file using a base64 file source instead of URL with Pdf.js?

Sometimes, we want to render a PDF file using a base64 file source instead of URL with Pdf.js.

In this article, we’ll look at how to render a PDF file using a base64 file source instead of URL with Pdf.js.

How to render a PDF file using a base64 file source instead of URL with Pdf.js?

To render a PDF file using a base64 file source instead of URL with Pdf.js, we convert the base64 URL into a Uint8Array.

For instance, we write

const BASE64_MARKER = ";base64,";

const convertDataURIToBinary = (dataURI) => {
  const base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
  const base64 = dataURI.substring(base64Index);
  const raw = window.atob(base64);
  const rawLength = raw.length;
  const array = new Uint8Array(new ArrayBuffer(rawLength));

  for (let i = 0; i < rawLength; i++) {
    array[i] = raw.charCodeAt(i);
  }
  return array;
};

const pdfAsDataUri = "data:application/pdf;base64,JVBERi0xLjUK...";
const pdfAsArray = convertDataURIToBinary(pdfAsDataUri);
PDFJS.getDocument(pdfAsArray);

to define the convertDataURIToBinary .

to get the base64 content with dataURI.substring(base64Index).

Then we call atob with base64 to decode the base64 string.

Next, we create a Uint8Array with an ArrayBuffer.

Then we put the decoded base64 content into the array Uint8Array.

Next, we call convertDataURIToBinary with pdfAsDataUri to convert the base64 URL to a Uint8Array.

And then we call getDocument with the Uint8Array to render it.

Conclusion

To render a PDF file using a base64 file source instead of URL with Pdf.js, we convert the base64 URL into a Uint8Array.

Categories
JavaScript Answers

How to replace non-numeric characters with JavaScript regular expressions?

Sometimes, we want to replace non-numeric characters with JavaScript regular expressions.

In this article, we’ll look at how to replace non-numeric characters with JavaScript regular expressions.

How to replace non-numeric characters with JavaScript regular expressions?

To replace non-numeric characters with JavaScript regular expressions, we can use the string replace method.

For instance, we write

const newStr = str.replace(/[^0-9\.]+/g, "");

to call str.replace with /[^0-9\.]+/g and an empty string to return a string without all the non-numeric characters in str.

We use [^0-9\.]+ to match non-numeric characters.

And we use g to make replace replace all matches.

Conclusion

To replace non-numeric characters with JavaScript regular expressions, we can use the string replace method.

Categories
JavaScript Answers

How to append child after element with JavaScript?

Sometimes, we want to append child after element with JavaScript.

In this article, we’ll look at how to append child after element with JavaScript.

How to append child after element with JavaScript?

To append child after element with JavaScript, we can use the insertBefore method.

For instance, we write

if (parentGuest.nextSibling) {
  parentGuest.parentNode.insertBefore(childGuest, parentGuest.nextSibling);
} else {
  parentGuest.parentNode.appendChild(childGuest);
}

to call parentGuest.parentNode.insertBefore to insert childGuest before parentGuest element’s next sibling to insert it after parentGuest if nextSibling exists.

Otherwise, we insert it as its last child with appendChild.

Conclusion

To append child after element with JavaScript, we can use the insertBefore method.