Categories
JavaScript Answers

How to sort DOM nodes with JavaScript?

Sometimes, we want to sort DOM nodes with JavaScript.

In this article, we’ll look at how to sort DOM nodes with JavaScript.

How to sort DOM nodes with JavaScript?

To sort DOM nodes with JavaScript, we convert the node list to an array.

For instance, we write

const list = document.querySelector("#test-list");

[...list.children]
  .sort((a, b) => (a.innerText > b.innerText ? 1 : -1))
  .forEach((node) => list.appendChild(node));

to select the element with ID test-list with querySelector.

Then we get the child elements of it as a node list with list.children.

Next, we convert the node list to an array with the spread operator.

And then we call sort with a callback to sort the nodes by their innerText values and return the sorted array.

Finally, we call forEach to loop through each node.

Conclusion

To sort DOM nodes with JavaScript, we convert the node list to an array.

Categories
JavaScript Answers

How to converting object to array with JavaScript?

Sometimes, we want to converting object to array with JavaScript.

In this article, we’ll look at how to converting object to array with JavaScript.

How to converting object to array with JavaScript?

To converting object to array with JavaScript, we use the Object.values method.

For instance, we write

const inputObj = { a: "foo", b: [1, 2, 3], c: null, z: 55 };
const arr = Object.values(inputObj);

to call Object.values with inputObj to return an array with all the property values in inputObj.

Conclusion

To converting object to array with JavaScript, we use the Object.values method.

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 *.