Categories
JavaScript Answers

How to convert uint8 array to base64 encoded string with JavaScript?

Sometimes, we want to convert uint8 array to base64 encoded string with JavaScript.

In this article, we’ll look at how to convert uint8 array to base64 encoded string with JavaScript.

How to convert uint8 array to base64 encoded string with JavaScript?

To convert uint8 array to base64 encoded string with JavaScript, we can use the FileReader constructor.

For instance, we write

const base64Arraybuffer = async (data) => {
  const base64url = await new Promise((r) => {
    const reader = new FileReader();
    reader.onload = () => r(reader.result);
    reader.readAsDataURL(new Blob([data]));
  });
  return base64url.split(",", 2)[1];
};

//...

await base64Arraybuffer(new Uint8Array([1, 2, 3, 100, 200]));

to define the base64Arraybuffer function that returns a promise with the blob that we convert from the data the Uint8Array into a base64 string.

We create the reader FileReader object and then call readAsDataURL with the blob value to read the blob into a base64 string.

Then we call r with reader.result to return the reader.result base64 string as the resolve value.

Then we call base64Arraybuffer in an async function with a Unit8Array as its argument to read it into a base64 string.

Conclusion

To convert uint8 array to base64 encoded string with JavaScript, we can use the FileReader constructor.

Categories
JavaScript Answers

How to format a rounded number to N decimals with JavaScript?

Sometimes, we want to format a rounded number to N decimals with JavaScript.

In this article, we’ll look at how to format a rounded number to N decimals with JavaScript.

How to format a rounded number to N decimals with JavaScript?

To format a rounded number to N decimals with JavaScript, we can use the number toFixed method.

For instance, we write

const myNumber = 2;
const numStr = myNumber.toFixed(2);

to call myNumber.toFixed with 2 to return myNumber formatted to 2 decimal places as a string.

Conclusion

To format a rounded number to N decimals with JavaScript, we can use the number toFixed method.

Categories
JavaScript Answers

How to create an img element with JavaScript?

Sometimes, we want to create an img element with JavaScript.

In this article, we’ll look at how to create an img element with JavaScript.

How to create an img element with JavaScript?

To create an img element with JavaScript, we can use the Image constructor.

For instance, we write

const img = new Image(1200, 300);
img.src = "https://picsum.photos/200/300";

to create an Image object with the width and height of the image as arguments.

And then we set its src property to the URL of the image to load.

Conclusion

To create an img element with JavaScript, we can use the Image constructor.

Categories
JavaScript Answers

How to use JavaScript array .reduce with async/await?

Sometimes, we want to use JavaScript array .reduce with async/await.

In this article, we’ll look at how to use JavaScript array .reduce with async/await.

How to use JavaScript array .reduce with async/await?

To use JavaScript array .reduce with async/await, we can use the awaity library.

To install it, we run

npm install awaity

Then we use it by writing

import reduce from "awaity/reduce";

const posts = await reduce(
  [1, 2, 3],
  async (posts, id) => {
    const res = await fetch("/api/posts/" + id);
    const post = await res.json();

    return {
      ...posts,
      [id]: post,
    };
  },
  {}
);

to call the reduce function with the array we’re reducing and a callback that returns a promise.

We use await to get the result from fetch and return an object with reduced value.

Conclusion

To use JavaScript array .reduce with async/await, we can use the awaity library.

Categories
JavaScript Answers

How to get full list of users with Mongoose and JavaScript?

Sometimes, we want to get full list of users with Mongoose and JavaScript.

In this article, we’ll look at how to get full list of users with Mongoose and JavaScript.

How to get full list of users with Mongoose and JavaScript?

To get full list of users with Mongoose and JavaScript, we can use the forEach method.

For instance, we write

server.get("/usersList", (req, res) => {
  User.find({}, (err, users) => {
    const userMap = {};

    users.forEach((user) => {
      userMap[user._id] = user;
    });

    res.send(userMap);
  });
});

to create the /usersList Express route.

In it, we call User.find with an empty object to get all users.

We get the users from the callback with the users parameter.

We call users.forEach to loop through each entry and put them in the userMap object.

user._id has the unique ID for each entry.

Conclusion

To get full list of users with Mongoose and JavaScript, we can use the forEach method.