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.

Categories
JavaScript Answers

How to get value at a specific index of array in JavaScript?

Sometimes, we want to get value at a specific index of array in JavaScript.

In this article, we’ll look at how to get value at a specific index of array in JavaScript.

How to get value at a specific index of array in JavaScript?

To get value at a specific index of array in JavaScript, we can use square brackets or the at method.

For instance, we write

const valueAtIndex1 = myValues[1];

to get the value at index 1 from the myValues array with square brackets.

We can do the same thing by calling at by writing

const valueAtIndex1 = myValues.at(1);

We can call at with negative indexes.

Negative indexes starts with -1 for the last item in an array.

Conclusion

To get value at a specific index of array in JavaScript, we can use square brackets or the at method.