Categories
JavaScript Answers

How to do alphanumeric check in JavaScript?

Sometimes, we want to do alphanumeric check in JavaScript.

In this article, we’ll look at how to do alphanumeric check in JavaScript.

How to do way to alphanumeric check in JavaScript?

To do alphanumeric check in JavaScript, we can use a regex.

For instance, we write

const isAlphanumeric = /^[a-z0-9]+$/i.test("abc");

to call ^[a-z0-9]+$/i.test with the string that we want to check if it’s alphanumeric or not.

We use ^[a-z0-9]+$ to check if the whole string only has letters and digits.

And we use i to check that in a case insensitive manner.

Conclusion

To do alphanumeric check in JavaScript, we can use a regex.

Categories
JavaScript Answers

How to remove all files from directory without removing directory in Node.js?

Sometimes, we want to remove all files from directory without removing directory in Node.js.

In this article, we’ll look at how to remove all files from directory without removing directory in Node.js.

How to remove all files from directory without removing directory in Node.js?

To remove all files from directory without removing directory in Node.js, we can use fs.readdir to read the file paths from a directory.

And then we use fs.unlink to delete each file with each path.

For instance, we write

const fs = require("fs");
const path = require("path");

const directory = "test";

fs.readdir(directory, (err, files) => {
  if (err) throw err;

  for (const file of files) {
    fs.unlink(path.join(directory, file), (err) => {
      if (err) throw err;
    });
  }
});

to call readdir with directory to read the file paths in directory.

Then we loop through the files paths in the callback to delete all the listed files.

In the loop, we call fs.unlink with the full path returned by path.join(directory, file) to delete the file.

Conclusion

To remove all files from directory without removing directory in Node.js, we can use fs.readdir to read the file paths from a directory.

And then we use fs.unlink to delete each file with each path.

Categories
JavaScript Answers

How to create multidimensional array with JavaScript?

Sometimes, we want to create multidimensional array with JavaScript.

In this article, we’ll look at how to create multidimensional array with JavaScript.

How to create multidimensional array with JavaScript?

To create multidimensional array with JavaScript, we can assign the values to the position with the given indexes.

For instance, we write

const numeric = [
  ["input1", "input2"],
  ["input3", "input4"],
];
numeric[0][0] == "input1";
numeric[0][1] == "input2";
numeric[1][0] == "input3";
numeric[1][1] == "input4";

by putting arrays into arrays like

const numeric = [
  ["input1", "input2"],
  ["input3", "input4"],
];

We also assign values to the positions we want with

numeric[0][0] == "input1";
numeric[0][1] == "input2";
numeric[1][0] == "input3";
numeric[1][1] == "input4";

where the first index is the position of the outer array and the 2nd is the position of the inner array.

Conclusion

To create multidimensional array with JavaScript, we can assign the values to the position with the given indexes.

Categories
JavaScript Answers

How to format numbers with JavaScript?

Sometimes, we want to format numbers with JavaScript.

In this article, we’ll look at how to format numbers with JavaScript.

How to format numbers with JavaScript?

To format numbers with JavaScript, we call the number toLocaleString method.

For instance, we write

const value = (100000).toLocaleString(undefined, { minimumFractionDigits: 2 });
console.log(value);

to call toLocaleString with { minimumFractionDigits: 2 } to return a number string with the number rounded to 2 decimal places.

Conclusion

To format numbers with JavaScript, we call the number toLocaleString method.

Categories
JavaScript Answers

How to remove multiple elements from array in JavaScript?

Sometimes, we want to remove multiple elements from array in JavaScript.

In this article, we’ll look at how to remove multiple elements from array in JavaScript.

How to remove multiple elements from array in JavaScript?

To remove multiple elements from array in JavaScript, we can use the array filter method.

For instance, we write

let valuesArr = ["v1", "v2", "v3", "v4", "v5"];
const removeValFrom = [0, 2, 4];
valuesArr = valuesArr.filter((value, index) => {
  return removeValFrom.indexOf(index) === -1;
});

to call valueArr.filter with a callback that checks if the index of the element in valuesArr is in the removeValFrom array which has the indexes of the items in valuesArr that we want to remove.

A new array with the items that we want to keep are included, so we assign the array back to valuesArr.

Conclusion

To remove multiple elements from array in JavaScript, we can use the array filter method.