Categories
JavaScript Answers

How to fix Bootstrap 4 dropdown menu not working with JavaScript?

Sometimes, we want to fix Bootstrap 4 dropdown menu not working with JavaScript.

In this article, we’ll look at how to fix Bootstrap 4 dropdown menu not working with JavaScript.

How to fix Bootstrap 4 dropdown menu not working with JavaScript?

To fix Bootstrap 4 dropdown menu not working with JavaScript, we should make sure all the required libraries are added.

For instance, we write

<script
  src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
  integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
  crossorigin="anonymous"
></script>
<script
  src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
  integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
  crossorigin="anonymous"
></script>
<script
  src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
  integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
  crossorigin="anonymous"
></script>

to add jQuery and popper.js before adding the Bootstrap JavaScript library.

Conclusion

To fix Bootstrap 4 dropdown menu not working with JavaScript, we should make sure all the required libraries are added.

Categories
JavaScript Answers

How to write regular expression for not allowing spaces in the input field with JavaScript?

Sometimes, we want to write regular expression for not allowing spaces in the input field with JavaScript.

In this article, we’ll look at how to write regular expression for not allowing spaces in the input field with JavaScript.

How to write regular expression for not allowing spaces in the input field with JavaScript?

To write regular expression for not allowing spaces in the input field with JavaScript, we use the \S pattern.

For instance, we write

const regExp = /^\S+$/;

to use the \S pattern to match non-space characters.

+ lets us match a group of non-space characters.

^ is the marker for the start of a word.

And $ is the marker for the end of a word.

Conclusion

To write regular expression for not allowing spaces in the input field with JavaScript, we use the \S pattern.

Categories
JavaScript Answers

How to filter array of objects whose any properties contains a value with JavaScript?

Sometimes, we want to filter array of objects whose any properties contains a value with JavaScript.

In this article, we’ll look at how to filter array of objects whose any properties contains a value with JavaScript.

How to filter array of objects whose any properties contains a value with JavaScript?

To filter array of objects whose any properties contains a value with JavaScript, we use the array filter method.

For instance, we write

const filterByValue = (array, value) => {
  return array.filter(
    (data) =>
      JSON.stringify(data).toLowerCase().indexOf(value.toLowerCase()) !== -1
  );
};

to call array.filter with a callback that checks if the stringified version of the data object in array has the value we’re looking for indexOf.

If indexOf returns something other than -1, then the array returned by filter includes the data object.

Conclusion

To filter array of objects whose any properties contains a value with JavaScript, we use the array filter method.

Categories
JavaScript Answers

How to get month name of last month using moment and JavaScript?

Sometimes, we want to get month name of last month using moment and JavaScript.

In this article, we’ll look at how to get month name of last month using moment and JavaScript.

How to get month name of last month using moment and JavaScript?

To get month name of last month using moment and JavaScript, we use the format method.

For instance, we write

const monthMinusOneName = moment()
  .subtract(1, "month")
  .startOf("month")
  .format("MMMM");

to call subtract to subtract 1 month from today’s date.

We call startOf to return the start date of the month.

And we call format with 'MMMM' to return the month name.

Conclusion

To get month name of last month using moment and JavaScript, we use the format method.

Categories
JavaScript Answers

How to convert base64 png data to JavaScript file objects?

Sometimes, we want to convert base64 png data to JavaScript file objects.

In this article, we’ll look at how to convert base64 png data to JavaScript file objects.

How to convert base64 png data to JavaScript file objects?

To convert base64 png data to JavaScript file objects, we use fetch.

For instance, we write

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

to call fetch with the image url to make a get request to get the image.

Then we get the image as an array buffer with arrayBuffer.

Next we put the array buffer into an array in the File constructor to convert the array buffer to a file with MIME type mimeType.

Conclusion

To convert base64 png data to JavaScript file objects, we use fetch.