Categories
JavaScript Answers

How to implement regions/code collapse in JavaScript?

Sometimes, we want to implement regions/code collapse in JavaScript.

In this article, we’ll look at how to implement regions/code collapse in JavaScript.

How to implement regions/code collapse in JavaScript?

To implement regions/code collapse in JavaScript, we add comments for region markers.

For instance, we write

//#region MyRegion1

const foo = () => {};

//#endregion

//#region MyRegion2

const bar = () => {};

//#endregion

to wrap our code with region markers.

We use #region to mark the start of a region and use #endregion to mark the end of a region.

Then we can expand or collapse the code in Visual Studio or Visual Studio Code.

Conclusion

To implement regions/code collapse in JavaScript, we add comments for region markers.

Categories
JavaScript Answers

How to add a new array element to a JSON object with JavaScript?

Sometimes, we want to add a new array element to a JSON object with JavaScript.

In this article, we’ll look at how to add a new array element to a JSON object with JavaScript.

How to add a new array element to a JSON object with JavaScript?

To add a new array element to a JSON object with JavaScript, we parse the JSON string into an object, add the item to the array in the object, and the convert the object back to a JSON string.

For instance, we write

let jsonStr =
  '{"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"}]}';

const obj = JSON.parse(jsonStr);
obj["theTeam"].push({ teamId: "4", status: "pending" });
jsonStr = JSON.stringify(obj);

to call JSON.parse to parse jsonStr into an object.

Then we call push to push a new entry into the obj["theTeam"] array.

Finally we call JSON.stringify to convert obj back into a JSON string.

Conclusion

To add a new array element to a JSON object with JavaScript, we parse the JSON string into an object, add the item to the array in the object, and the convert the object back to a JSON string.

Categories
JavaScript Answers

How to check if character is number with JavaScript?

Sometimes, we want to check if character is number with JavaScript.

In this article, we’ll look at how to check if character is number with JavaScript.

How to check if character is number with JavaScript?

To check if character is number with JavaScript, we can use the regex test method.

For instance, we write

const isNumeric = (str) => {
  return /^\d+$/.test(str);
};

to define the isNumeric function that checks if str is a number with /^\d+$/.test.

\d+ matches strings that has all digits inside.

Conclusion

To check if character is number with JavaScript, we can use the regex test method.

Categories
JavaScript Answers

How to calculate the day of the year with JavaScript?

Sometimes, we want to calculate the day of the year with JavaScript.

In this article, we’ll look at how to calculate the day of the year with JavaScript.

How to calculate the day of the year with JavaScript?

To calculate the day of the year with JavaScript, we can calculate the difference between the current datetime and the midnight of the first day of the year.

For instance, we write

const now = new Date();
const start = new Date(now.getFullYear(), 0, 0);
const diff =
  now -
  start +
  (start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000;
const oneDay = 1000 * 60 * 60 * 24;
const day = Math.floor(diff / oneDay);

to calculate diff by subtracting now by start.

And then we add back the time zone offset difference to add back the difference caused by daylight saving time.

Then we divide diff by oneDay, which is 1 day in milliseconds.

Finally, we call Math.floor to round the quotient down.

Conclusion

To calculate the day of the year with JavaScript, we can calculate the difference between the current datetime and the midnight of the first day of the year.

Categories
JavaScript Answers

How to split JavaScript string on upper case characters?

Sometimes, we want to split JavaScript string on upper case characters.

In this article, we’ll look at how to split JavaScript string on upper case characters.

How to split JavaScript string on upper case characters?

To split JavaScript string on upper case characters, we can call the string split method with a regex.

For instance, we write

const a = "ThisIsTheStringToSplit".split(/(?=[A-Z])/);

to call split with /(?=[A-Z])/ to split the string on upper case characters.

We use ?= to keep the whole word intact after splitting.

split returns an array with the split strings.

Conclusion

To split JavaScript string on upper case characters, we can call the string split method with a regex.