Categories
JavaScript Answers

How to decode UTF-8 with JavaScript?

Sometimes, we want to decode UTF-8 with JavaScript.

In this article, we’ll look at how to decode UTF-8 with JavaScript.

How to decode UTF-8 with JavaScript?

To decode UTF-8 with JavaScript, we use the decodeURIComponent function.

For instance, we write

const decodeUtf8 = (s) => {
  return decodeURIComponent(escape(s));
};

to define the decodeUtf8 function.

In it, we call escape to return a escaped version of string s.

And then we call decodeURIComponent to decode the contents of the escaped string.

Conclusion

To decode UTF-8 with JavaScript, we use the decodeURIComponent function.

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.