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.

Categories
JavaScript Answers

How to check if a string contains any element of an array in JavaScript?

Sometimes,. we want to check if a string contains any element of an array in JavaScript.

In this article, we’ll look at how to check if a string contains any element of an array in JavaScript.

How to check if a string contains any element of an array in JavaScript?

To check if a string contains any element of an array in JavaScript, we call the array some method.

For instance, we write

const arr = ["banana", "monkey banana", "apple", "kiwi", "orange"];

const checker = (value) =>
  !["banana", "apple"].some((element) => value.includes(element));

console.log(arr.filter(checker));

to call arr.filter with a callback that checks any of 'banana' or 'apple' isn’t in the value of element being looped through in the arr array.

As a result, filter returns an array with values that don’t have 'banana' or 'apple' in arr.

Conclusion

To check if a string contains any element of an array in JavaScript, we call the array some method.