Categories
JavaScript Answers

How to sort a ES6 map object with JavaScript?

Sometimes, we want to sort a ES6 map object with JavaScript.

In this article, we’ll look at how to sort a ES6 map object with JavaScript.

How to sort a ES6 map object with JavaScript?

To sort a ES6 map object with JavaScript, we convert the map to an array, sort that, and then convert the sorted array back into a map.

For instance, we write

const sortedMap = new Map(
  [...map].sort((a, b) => {
    // ...
  })
);

to convert the map to an array with [...map].

Then we call sort with a callback to sort the array.

And then we convert the sorted array back to a map using the Map constructor.

Conclusion

To sort a ES6 map object with JavaScript, we convert the map to an array, sort that, and then convert the sorted array back into a map.

Categories
JavaScript Answers

How to properly export an ES6 class in Node.js?

Sometimes, we want to properly export an ES6 class in Node.js.

In this article, we’ll look at how to properly export an ES6 class in Node.js.

How to properly export an ES6 class in Node.js?

To properly export an ES6 class in Node.js, we can set the class as the value of module.exports in a module.

For instance, we write

module.exports = class Person {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  display() {
    console.log(this.firstName, this.lastName);
  }
};

to export the Person class in person.js.

Then we write

const Person = require("./person.js");

const someone = new Person("First name", "Last name");
someone.display();

to import the class with

const Person = require("./person.js");

And then we create a Person object and call the display method.

Conclusion

To properly export an ES6 class in Node.js, we can set the class as the value of module.exports in a module.

Categories
JavaScript Answers

How to convert special characters to HTML in JavaScript

Sometimes, we want to convert special characters to HTML in JavaScript.

In this article, we’ll look at how to convert special characters to HTML in JavaScript.

How to convert special characters to HTML in JavaScript

To convert special characters to HTML in JavaScript, we use the String.fromCharCode method.

For instance, we write

const decodeHtmlCharCodes = (str) =>
  str.replace(/(&#(\d+);)/g, (match, capture, charCode) =>
    String.fromCharCode(charCode)
  );

to define the decodeHtmlCharCodes function.

In it, we call str.replace with a regex to match all special characters with the regex.

The callback return the character given the charCode, which we get from the regex matches.

Conclusion

To convert special characters to HTML in JavaScript, we use the String.fromCharCode method.

Categories
JavaScript Answers

How to run script elements inserted with innerHTML with JavaScript?

Sometimes, we want to run script elements inserted with innerHTML with JavaScript

In this article, we’ll look at how to run script elements inserted with innerHTML with JavaScript.

How to run script elements inserted with innerHTML with JavaScript?

To run script elements inserted with innerHTML with JavaScript, we append the script element to the body element.

For instance, we write

const script = document.createElement("script");
script.innerHTML = 'console.log("hi")';
document.body.appendChild(script);

to create a script element with createElement.

Then we call document.body.appendChild to append the script element as the last child of the body element and run it.

Conclusion

To run script elements inserted with innerHTML with JavaScript, we append the script element to the body element.

Categories
JavaScript Answers

How to check if the response of a fetch is a JSON object in JavaScript?

Sometimes, we want to check if the response of a fetch is a JSON object in JavaScript.

In this article, we’ll look at how to check if the response of a fetch is a JSON object in JavaScript.

How to check if the response of a fetch is a JSON object in JavaScript?

To check if the response of a fetch is a JSON object in JavaScript, we can check the content-type response header.

For instance, we write

const response = await fetch(myRequest);
const contentType = response.headers.get("content-type");
if (contentType?.indexOf("application/json") !== -1) {
  //...
} else {
  //...
}

in an async function to call fetch to make a request.

Then we get the content-type header with response.headers.get("content-type").

And then we check if its value includes 'application/json'.

If it does, then the response should be JSON.

Conclusion

To check if the response of a fetch is a JSON object in JavaScript, we can check the content-type response header.