Categories
JavaScript Answers

How to add a year to today’s date with JavaScript?

Sometimes, we want to add a year to today’s date with JavaScript.

In this article, we’ll look at how to add a year to today’s date with JavaScript.

How to add a year To today’s date with JavaScript?

To add a year to today’s date with JavaScript, we can use the date’s getFullYear and setFullYear methods.

For instance, we write

const aYearFromNow = new Date();
aYearFromNow.setFullYear(aYearFromNow.getFullYear() + 1);
console.log(aYearFromNow);

to create the aYearFromNow Date object with the current date and time.

Then we call aYearFromNow.getFullYear to get the 4 digit year number of the date.

Then we add 1 to it and use the sum as the argument for setFullYear to increment the year of aYearFromNow by 1.

Conclusion

To add a year to today’s date with JavaScript, we can use the date’s getFullYear and setFullYear methods.

Categories
JavaScript Answers

How to sort 2 dimensional array by column value with JavaScript?

Sometimes, we want to sort 2 dimensional array by column value with JavaScript.

In this article, we’ll look at how to sort 2 dimensional array by column value with JavaScript.

How to sort 2 dimensional array by column value with JavaScript?

To sort 2 dimensional array by column value with JavaScript, we can use the array sort method.

For instance, we write

const arr = [
  [12, "AAA"],
  [12, "BBB"],
  [12, "CCC"],
  [28, "DDD"],
  [18, "CCC"],
  [12, "DDD"],
  [18, "CCC"],
  [28, "DDD"],
  [28, "DDD"],
  [58, "BBB"],
  [68, "BBB"],
  [78, "BBB"],
];

const sortedArr = arr.sort((a, b) => {
  return a[0] - b[0];
});

to call arr.sort with a callback that sorts each array in arr by the value of the first entry in each array in ascending order.

a and b are entries in arr.

Conclusion

To sort 2 dimensional array by column value with JavaScript, we can use the array sort method.

Categories
JavaScript Answers

How to remove non-ASCII character in string with JavaScript?

Sometimes, we want to remove non-ASCII character in string with JavaScript.

In this article, we’ll look at how to remove non-ASCII character in string with JavaScript.

How to remove non-ASCII character in string with JavaScript?

To remove non-ASCII character in string with JavaScript, we use the string’s replace method.

For instance, we write

const newStr = str.replace(/[^\x00-\x7F]/g, "");

to call str.replace with the /[^\x00-\x7F]/g regex to replace all characters that don’t have character code between 0 and 127 with empty strings.

We use the g flag to find all matches of the regex pattern in str.

Conclusion

To remove non-ASCII character in string with JavaScript, we use the string’s replace method.

Categories
JavaScript Answers

How to create JSON string in JavaScript?

Sometimes, we want to create JSON string in JavaScript.

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

How to create JSON string in JavaScript?

To create JSON string in JavaScript, we can create an object and then call JSON.stringify with it.

For instance, we write

const obj = {};
obj.name = "bob";
obj.age = 32;
obj.married = false;

const string = JSON.stringify(obj);

to create the obj object.

Then we add a few properties into it with

obj.name = "bob";
obj.age = 32;
obj.married = false;

Then we call JSON.stringify with obj to convert obj to a JSON string.

Conclusion

To create JSON string in JavaScript, we can create an object and then call JSON.stringify with it.

Categories
JavaScript Answers

How to fix Node.js can’t create Blobs with JavaScript?

Sometimes, we want to fix Node.js can’t create Blobs with JavaScript.

In this article, we’ll look at how to fix Node.js can’t create Blobs with JavaScript.

How to fix Node.js can’t create Blobs with JavaScript?

To fix Node.js can’t create Blobs with JavaScript, we can import it from the node:buffer module since Node 16.

To use it, we write

import { Blob } from "node:buffer";

const b = new Blob([]);

to import the Blob constructor with

import { Blob } from "node:buffer";

Then we use it to create the b Blob object.

We can use the cross-blob module if our app runs with older versions of Node.

To install it, we run npm i cross-blob.

Then we use it by writing

import Blob from "cross-blob";

const b = new Blob([]);

Conclusion

To fix Node.js can’t create Blobs with JavaScript, we can import it from the node:buffer module since Node 16.