Categories
JavaScript Answers

How to sort array of objects by a boolean property with JavaScript?

Sometimes, we want to sort array of objects by a boolean property with JavaScript.

In this article, we’ll look at how to sort array of objects by a boolean property with JavaScript.

How to sort array of objects by a boolean property with JavaScript?

To sort array of objects by a boolean property with JavaScript, we can use the Number function.

For instance, we write

const a = [
  false,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  false,
  true,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
];
const sorted = a.sort((x, y) => Number(x) - Number(y));
console.log(a);

to call a.sort with a callback that converts the booleans x and y to numbers.

And we subtract them so that we sort items with true coming before false.

Conclusion

To sort array of objects by a boolean property with JavaScript, we can use the Number function.

Categories
JavaScript Answers

How to get locale short date format using JavaScript?

Sometimes, we want to get locale short date format using JavaScript.

In this article, we’ll look at how to get locale short date format using JavaScript.

How to get locale short date format using JavaScript?

To get locale short date format using JavaScript, we use the toLocaleDateString method.

For instance, we write

const date = new Date();

const options = {
  weekday: "short",
  year: "numeric",
  month: "2-digit",
  day: "numeric",
};

console.log(date.toLocaleDateString("en", options));

to call toLocaleDateString with the locale string and the options which sets the returned date string to have the short date format.

Conclusion

To get locale short date format using JavaScript, we use the toLocaleDateString method.

Categories
JavaScript Answers

How to get first and last day of the current week in JavaScript?

Sometimes, we want to get first and last day of the current week in JavaScript.

In this article, we’ll look at how to get first and last day of the current week in JavaScript.

How to get first and last day of the current week in JavaScript?

To get first and last day of the current week in JavaScript, we can use some date methods.

For instance, we write

const curr = new Date();
const first = curr.getDate() - curr.getDay();
const last = first + 6;

const firstDay = new Date(curr.setDate(first)).toUTCString();
const lastDay = new Date(curr.setDate(last)).toUTCString();

to create the curr date with the current datetime.

And then we get the difference between the first day of the week and the current day of the month with

curr.getDate() - curr.getDay()

And then we get the last day of the week with first + 6.

Next, we use setDate with first and last to get the first and last day of the week.

And then we create a new Date object from the dates.

We use toUTCString to return human readable date strings for each date.

Conclusion

To get first and last day of the current week in JavaScript, we can use some date methods.

Categories
JavaScript Answers

How to fix Bootstrap popover not showing on top of all elements with JavaScript?

Sometimes, we want to fix Bootstrap popover not showing on top of all elements with JavaScript.

In this article, we’ll look at how to fix Bootstrap popover not showing on top of all elements with JavaScript.

How to fix Bootstrap popover not showing on top of all elements with JavaScript?

To fix Bootstrap popover not showing on top of all elements with JavaScript, we call tooltip with an object with the container option.

For instance, we write

$("#element").tooltip({ container: "body" });

to call tooltip with { container: "body" } to set the container of the tooltip to the body element.

This will make sure that the tooltip shows on top of any element on the page.

Conclusion

To fix Bootstrap popover not showing on top of all elements with JavaScript, we call tooltip with an object with the container option.

Categories
JavaScript Answers

How to change the browser zoom level with JavaScript?

Sometimes, we want to change the browser zoom level with JavaScript.

In this article, we’ll look at how to change the browser zoom level with JavaScript.

How to change the browser zoom level with JavaScript?

To change the browser zoom level with JavaScript, we set the zoom style.

For instance, we write

document.body.style.zoom = "80%";

to set the zoom style of the body element to '80%' to set the zoom of the page to 80%.

Conclusion

To change the browser zoom level with JavaScript, we set the zoom style.