Categories
JavaScript Answers

How to add optional first argument in function with JavaScript?

Sometimes, we want to add optional first argument in function with JavaScript.

In this article, we’ll look at how to add optional first argument in function with JavaScript.

How to add optional first argument in function with JavaScript?

To add optional first argument in function with JavaScript, we can make our function accept an object.

For instance, we write

const myFunction = (hash) => {
  const { options, content } = hash;
  //...
};

myFunction({ options, content });

to define the myFunction function that takes the hash object.

In it, we get the options and content properties from the hash.

And we call myFunction with an object with those 2 properties.

We can omit any property in the object we call myFunction with.

Conclusion

To add optional first argument in function with JavaScript, we can make our function accept an object.

Categories
React Answers

How to sort an array of objects in React and render them?

Sometimes, we want to sort an array of objects in React and render them.

In this article, we’ll look at how to sort an array of objects in React and render them.

How to sort an array of objects in React and render them?

To sort an array of objects in React and render them, we call sort before we call map.

For instance, we write

const sorted = [...data]
  .sort((a, b) => a.timeM - b.timeM)
  .map((item, i) => (
    <div key={i}>
      {item.matchID}
      {item.timeM} {item.description}
    </div>
  ));

to call sort with a callback that sorts by the timeM value of each object in data in ascending order.

Then we call map with a callback that returns the rendered items.

Conclusion

To sort an array of objects in React and render them, we call sort before we call map.

Categories
JavaScript Answers

How to clear an HTML file upload field via JavaScript?

Sometimes, we want to clear an HTML file upload field via JavaScript.

In this article, we’ll look at how to clear an HTML file upload field via JavaScript.

How to clear an HTML file upload field via JavaScript?

To clear an HTML file upload field via JavaScript, we set the file input’s value property to an empty string.

For instance, we write

<form>
  <input id="fileInput" name="fileInput" type="file" />
</form>

to add a form with a file input.

Then we write

document.getElementById("fileInput").value = "";

to select the file input with getElementById.

And we set its value property to an empty string.

Conclusion

To clear an HTML file upload field via JavaScript, we set the file input’s value property to an empty string.

Categories
JavaScript Answers

How to add space between numbers with JavaScript?

Sometimes, we want to add space between numbers with JavaScript.

In this article, we’ll look at how to add space between numbers with JavaScript.

How to add space between numbers with JavaScript?

To add space between numbers with JavaScript, we use the number toLocaleString method.

For instance, we write

const num = 1234567890;
const result = num.toLocaleString("fr");

to call num.toLocaleString with "fr" to return a number in the Frenchlocale.

This will return '1 234 567 890'.

Conclusion

To add space between numbers with JavaScript, we use the number toLocaleString method.

Categories
JavaScript Answers

How to use string replace with regex to strip off illegal characters with JavaScript?

Sometimes, we want to use string replace with regex to strip off illegal characters with JavaScript.

In this article, we’ll look at how to use string replace with regex to strip off illegal characters with JavaScript.

How to use string replace with regex to strip off illegal characters with JavaScript?

To use string replace with regex to strip off illegal characters with JavaScript, we define a regex that match all the characters we want to remove.

For instance, we write

const cleanString = dirtyString.replace(/[|&;$%@"<>()+,]/g, "");

to call dirtyString.replace with a regex that matches anything in the square brackets.

The g flag makes replace replace all matches.

We replace each match with an empty string.

Conclusion

To use string replace with regex to strip off illegal characters with JavaScript, we define a regex that match all the characters we want to remove.