Categories
JavaScript Answers

How to create structs in JavaScript?

Sometimes, we want to create structs in JavaScript.

In this article, we’ll look at how to create structs in JavaScript.

How to create structs in JavaScript?

To create structs in JavaScript, we can create object literals.

For instance, we write

const obj = { id: 1, speaker: "john", country: "au" };

to create the obj object with various properties to store data like structs.

Conclusion

To create structs in JavaScript, we can create object literals.

Categories
JavaScript Answers

How to POST with multipart form data using fetch and JavaScript?

Sometimes, we want to POST with multipart form data using fetch and JavaScript.

In this article, we’ll look at how to POST with multipart form data using fetch and JavaScript.

How to POST with multipart form data using fetch and JavaScript?

To POST with multipart form data using fetch and JavaScript, we can send a FormData object as its body.

For instance, we write

const sendData = async (url, data) => {
  const formData = new FormData();

  for (const [key, value] of Object.entries(data)) {
    formData.append(key, value);
  }

  const response = await fetch(url, {
    method: "POST",
    body: formData,
  });

  // ...
};

to create the senData function.

In it, we create a FormData object.

And then we use the for-of loop with Object.entries to loop through the keys and values of each property in data and add them to the formData object with append.

Then we call fetch to make a post request to the url with body set to formData.

Conclusion

To POST with multipart form data using fetch and JavaScript, we can send a FormData object as its body.

Categories
JavaScript Answers

How to fix JavaScript “Not a Constructor” Exception while creating objects?

Sometimes, we want to fix JavaScript "Not a Constructor" Exception while creating objects.

In this article, we’ll look at how to fix JavaScript "Not a Constructor" Exception while creating objects.

How to fix JavaScript "Not a Constructor" Exception while creating objects?

To fix JavaScript "Not a Constructor" Exception while creating objects, we should create constructors with regular functions or the class syntax.

For instance, instead of writing

const F = () => {};
const f = new F();

We write

class F {}
const f = new F();

to replace the arrow function with class F.

class is syntactic sugar for constructor functions.

Conclusion

To fix JavaScript "Not a Constructor" Exception while creating objects, we should create constructors with regular functions or the class syntax.

Categories
JavaScript Answers

How to sort array of objects by a boolean property with JavaScript?

Sometimes, we want to sort array of objects by a boolean property with JavaScript.

In this article, we’ll look at how to sort array of objects by a boolean property with JavaScript.

How to sort array of objects by a boolean property with JavaScript?

To sort array of objects by a boolean property with JavaScript, we can use the Number function.

For instance, we write

const a = [
  false,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  false,
  true,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
];
const sorted = a.sort((x, y) => Number(x) - Number(y));
console.log(a);

to call a.sort with a callback that converts the booleans x and y to numbers.

And we subtract them so that we sort items with true coming before false.

Conclusion

To sort array of objects by a boolean property with JavaScript, we can use the Number function.

Categories
JavaScript Answers

How to get locale short date format using JavaScript?

Sometimes, we want to get locale short date format using JavaScript.

In this article, we’ll look at how to get locale short date format using JavaScript.

How to get locale short date format using JavaScript?

To get locale short date format using JavaScript, we use the toLocaleDateString method.

For instance, we write

const date = new Date();

const options = {
  weekday: "short",
  year: "numeric",
  month: "2-digit",
  day: "numeric",
};

console.log(date.toLocaleDateString("en", options));

to call toLocaleDateString with the locale string and the options which sets the returned date string to have the short date format.

Conclusion

To get locale short date format using JavaScript, we use the toLocaleDateString method.