Categories
JavaScript Answers

How to capture ctrl+z key combination in JavaScript?

Sometimes, we want to capture ctrl+z key combination in JavaScript.

In this article, we’ll look at how to capture ctrl+z key combination in JavaScript.

How to capture ctrl+z key combination in JavaScript?

To capture ctrl+z key combination in JavaScript, we can listen to the keydown event.

For instance, we write

document.addEventListener("keydown", (event) => {
  if (event.ctrlKey && event.key === "z") {
    console.log("Undo!");
  }
});

to listen for the keydown event on the page with document.addEventListener.

In the event handler callback, we check if the ctrl key is pressed with event.ctrlKey.

And we check if the z key is pressed with event.key === "z".

If they’re both true, then ctrl+z is pressed.

Conclusion

To capture ctrl+z key combination in JavaScript, we can listen to the keydown event.

Categories
JavaScript Answers

How to check if one date is between two dates with JavaScript?

Sometimes, we want to check if one date is between two dates with JavaScript.

In this article, we’ll look at how to check if one date is between two dates with JavaScript.

How to check if one date is between two dates with JavaScript?

To check if one date is between two dates with JavaScript, we can compare their timestamps.

For instance, we write

const currentDate = new Date().toJSON().slice(0, 10);
const from = new Date("2022/01/01");
const to = new Date("2022/01/31");
const check = new Date(currentDate);

console.log(check > from && check < to);

to get the currentDate date string with the new Date().toJSON().slice(0, 10).

Then we create the from and to dates with the Date constructor.

We then create check Date object with the currentDate string.

Then we check if check is between from and to with

check > from && check < to

When we use comparison operators with dates, they’ll be converted to timestamps in milliseconds before we compare them, so we can compare them directly.

Conclusion

To check if one date is between two dates with JavaScript, we can compare their timestamps.

Categories
JavaScript Answers

How to get key with the highest value from object with JavaScript?

Sometimes, we want to get key with the highest value from object with JavaScript.

In this article, we’ll look at how to get key with the highest value from object with JavaScript.

How to get key with the highest value from object with JavaScript?

To get key with the highest value from object with JavaScript, we can use the Objec.entries method.

For instance, we write

const data = { a: 1, b: 2, undefined: 3 };
const maxValue = Object.entries(data).sort((x, y) => y[1] - x[1])[0];

to call Object.entries with data to return an array with array of key-value pairs.

Then we call sort with a callback to sort each key-value pair array by its value.

And we get the first element from the sorted array which has the highest value in the data object.

Conclusion

To get key with the highest value from object with JavaScript, we can use the Objec.entries method.

Categories
JavaScript Answers

How to do a case insensitive sorting on a collection using Lodash orderBy with JavaScript?

Sometimes, we want to do a case insensitive sorting on a collection using Lodash orderBy with JavaScript.

In this article, we’ll look at how to do a case insensitive sorting on a collection using Lodash orderBy with JavaScript.

How to do a case insensitive sorting on a collection using Lodash orderBy with JavaScript?

To do a case insensitive sorting on a collection using Lodash orderBy with JavaScript, we can convert all the values to lower case before we compare them with orderBy.

For instance, we write

const users = [
  { name: "A", age: 48 },
  { name: "B", age: 34 },
  { name: "b", age: 40 },
  { name: "a", age: 36 },
];

const sortedUsers = _.orderBy(
  users,
  [(user) => user.name.toLowerCase()],
  ["desc"]
);
console.log(sortedUsers);

to call orderBy with the users array, an array with a function that convert each object’s name property to lower case, and an array with the order that we want to sort by.

We convert each name property to lower case with the toLowerCase method.

A new array with the sorted values is returned.

Conclusion

To do a case insensitive sorting on a collection using Lodash orderBy with JavaScript, we can convert all the values to lower case before we compare them with orderBy.

Categories
JavaScript Answers

How to change iframe src with JavaScript?

Sometimes, we want to change iframe src with JavaScript.

In this article, we’ll look at how to change iframe src with JavaScript.

How to change iframe src with JavaScript?

To change iframe src with JavaScript, we set the src property of the iframe element.

For instance, we write

document.getElementById("calendar").src = loc;

to select the iframe with ID calendar with getElementById.

Then we set the src attribute of the iframe by setting the src property of the iframe to the loc URL string.

Conclusion

To change iframe src with JavaScript, we set the src property of the iframe element.