Categories
JavaScript Answers

How to get the name of an HTML page in JavaScript?

Sometimes, we want to get the name of an HTML page in JavaScript.

In this article, we’ll look at how to get the name of an HTML page in JavaScript.

How to get the name of an HTML page in JavaScript?

To get the name of an HTML page in JavaScript, we can use the split and slice methods.

For instance, we write

const fileName = location.href.split("/").slice(-1);

to get the current page URL with location.href.

Then we call split with '/' to return an array of URL segments.

And we get the last segment, which has the file name with slice called with -1.

Conclusion

To get the name of an HTML page in JavaScript, we can use the split and slice methods.

Categories
JavaScript Answers

How to use filter with nested objects with Lodash?

Sometimes, we want to use filter with nested objects with Lodash.

In this article, we’ll look at how to use filter with nested objects with Lodash.

How to use filter with nested objects with Lodash?

To use filter with nested objects with Lodash, we can filter with the nested property values.

For instance, we write

const filtered = _.filter(summary.data, ["category.parent", "Food"]);

to call filter with the summary.data array and an array with the property path and the value to check for in the property path.

Therefore, filtered is an array with the entries in summary.data that has the category.parent property set to 'Food'.

Conclusion

To use filter with nested objects with Lodash, we can filter with the nested property values.

Categories
JavaScript Answers

How to import all types with TypeScript or JavaScript?

Sometimes, we want to import all types with TypeScript or JavaScript.

In this article, we’ll look at how to import all types with TypeScript or JavaScript.

How to import all types with TypeScript or JavaScript?

To import all types with TypeScript or JavaScript, we can use *.

For instance, we write

import * as foo from "./otherClass";

to import all the exported contents as the foo object from the otherClass module.

Then we can use the imports by writing

foo.ClassA

to use ClassA from the otherClass module.

Conclusion

To import all types with TypeScript or JavaScript, we can use *.

Categories
JavaScript Answers

How to determine if a checkbox is checked with JavaScript?

Sometimes, we want to determine if a checkbox is checked with JavaScript.

In this article, we’ll look at how to determine if a checkbox is checked with JavaScript.

How to determine if a checkbox is checked with JavaScript?

To determine if a checkbox is checked with JavaScript, we use the checked property.

For instance, we write

<label><input id="lifecheck" type="checkbox" />Lives</label>

to add a checkbox.

Then we write

const checked = document.getElementById("lifecheck").checked;

to select the checkbox with getElementById.

And then we check if it’s checked with the checked property.

Conclusion

To determine if a checkbox is checked with JavaScript, we use the checked property.

Categories
JavaScript Answers

How to split array into two arrays with JavaScript?

Sometimes, we want to split array into two arrays with JavaScript.

In this article, we’ll look at how to split array into two arrays with JavaScript.

How to split array into two arrays with JavaScript?

To split array into two arrays with JavaScript, we can use the array slice method.

For instance, we write

const arr = ["a", "b", "c", "d", "e", "f"];

const indexToSplit = arr.indexOf("c");
const first = arr.slice(0, indexToSplit);
const second = arr.slice(indexToSplit + 1);

to call indexOf to get the index of 'c'.

Then we call slice with 0 and indexToSplit to return an array with arr entries from index 0 to indexToSplit - 1.

Next we call slice again with indexToSplit + 1 to return an array from index indexToSplit + 1 to the end of the arr array.

Conclusion

To split array into two arrays with JavaScript, we can use the array slice method.