Categories
JavaScript Answers

How to create an ISO date object in JavaScript?

Sometimes, we want to create an ISO date object in JavaScript.

In this article, we’ll look at how to create an ISO date object in JavaScript.

How to create an ISO date object in JavaScript?

To create an ISO date object in JavaScript, we use the toISOString method.

For instance, we write

const isoDate = new Date().toISOString();

to create a Date object with the current date and time.

Then we call toISOString on it to return a string with the current ISO date and time.

Conclusion

To create an ISO date object in JavaScript, we use the toISOString method.

Categories
JavaScript Answers

How to convert Map to array of object with JavaScript?

Sometimes, we want to convert Map to array of object with JavaScript.

In this article, we’ll look at how to convert Map to array of object with JavaScript.

How to convert Map to array of object with JavaScript?

To convert Map to array of object with JavaScript, we use the spread operator.

For instance, we write

const myMap = new Map().set("a", 1).set("b", 2);
const arr = [...myMap].map(([name, value]) => ({ name, value }));
console.log(arr);

to create a map and add some entries to it with set.

Next, we spread myMap into an array with the spread operator.

Then we call map with a callback that returns an object with the name and value properties, which is the key and value respectively.

Conclusion

To convert Map to array of object with JavaScript, we use the spread operator.

Categories
JavaScript Answers

How to add an ID to dynamically created div with JavaScript?

Sometimes, we want to add an ID to dynamically created div with JavaScript.

In this article, we’ll look at how to add an ID to dynamically created div with JavaScript.

How to add an ID to dynamically created div with JavaScript?

To add an ID to dynamically created div with JavaScript, we set the id property.

For instance, we write

cartDiv.id = "someID";

to set the cartDiv div’s ID by setting its id property to the ID name string we want.

Conclusion

To add an ID to dynamically created div with JavaScript, we set the id property.

Categories
JavaScript Answers

How to convert data URL to file object in JavaScript?

Sometimes, we want to convert data URL to file object in JavaScript.

In this article, we’ll look at how to convert data URL to file object in JavaScript.

How to convert data URL to file object in JavaScript?

To convert data URL to file object in JavaScript, we use fetch.

For instance, we write

const res = await fetch(src);
const buf = await res.arrayBuffer();
const file = new File([buf], fileName, { type: mimeType });

to call fetch with the src data URL string.

Then we call res.arrayBuffer to get an array buffer from the URL.

Next we create a File object with the buf buffer, fileName string, and the mimeType MIME type string.

Conclusion

To convert data URL to file object in JavaScript, we use fetch.

Categories
JavaScript Answers

How to time out a Promise if failed to complete in time with Node.js and JavaScript?

Sometimes, we want to time out a Promise if failed to complete in time with Node.js and JavaScript.

In this article, we’ll look at how to time out a Promise if failed to complete in time with Node.js and JavaScript.

How to time out a Promise if failed to complete in time with Node.js and JavaScript?

To time out a Promise if failed to complete in time with Node.js and JavaScript, we use the Promise.race method.

For instance, we write

const withTimeout = (millis, promise) => {
  const timeout = new Promise((resolve, reject) =>
    setTimeout(() => reject(`Timed out after ${millis} ms.`), millis)
  );
  return Promise.race([promise, timeout]);
};

to define the withTimeout function.

In it, we create the timeout promise, which calls setTimeout callback when millis milliseconds has elapsed.

We call reject to reject the promise.

Then we call Promise.race with an array with the promise we want to run and the timeout promise.

Then the result of the promise that finishes first is returned as a promise.

Conclusion

To time out a Promise if failed to complete in time with Node.js and JavaScript, we use the Promise.race method.