Categories
JavaScript Answers

How to sort an array so that null values always come last with JavaScript?

Sometimes, we want to sort an array so that null values always come last with JavaScript.

In this article, we’ll look at how to sort an array so that null values always come last with JavaScript.

How to sort an array so that null values always come last with JavaScript?

To sort an array so that null values always come last with JavaScript, we can use the array sort method.

For instance, we write

const sorted = arr.sort(
  (a, b) => (a !== null ? a : Infinity) - (b !== null ? b : Infinity)
);

to call arr.sort with a callback that sorts non-null values in ascending order.

We check if a or b is null.

If they are, we return Infinity as its value and use that for sorting.

To sort in descending order, we flip the expressions in the callback by writing

const sorted = arr.sort(
  (a, b) => (b !== null ? b : -Infinity) - (a !== null ? a : -Infinity)
);

Conclusion

To sort an array so that null values always come last with JavaScript, we can use the array sort method.

Categories
JavaScript Answers

How to convert a char array to a string with JavaScript?

Sometimes, we want to convert a char array to a string with JavaScript.

In this article, we’ll look at how to convert a char array to a string with JavaScript.

How to convert a char array to a string with JavaScript?

To convert a char array to a string with JavaScript, we use the array join method.

For instance, we write

const str = s.join("");

to call s.join with an empty string to join the character strings in array s into the str string.

join takes a separator between the strings, so passing in an empty string means there’s no separator between the characters.

Conclusion

To convert a char array to a string with JavaScript, we use the array join method.

Categories
React Answers

How to add CSS pseudo elements in React?

Sometimes, we want to add CSS pseudo elements in React.

In this article, we’ll look at how to add CSS pseudo elements in React.

How to add CSS pseudo elements in React?

To add CSS pseudo elements in React, we add real elements.

For instance, we write

<div>
  <span>Something</span>
  <div
    style={{ position: "absolute", WebkitFilter: "blur(10px) saturate(2)" }}
  />
</div>

in our React component, which is equivalent to

<div class="something"><span>Something</span></div>
<style>
  .something::after {
    content: "";
    position: absolute;
    -webkit-filter: blur(10px) saturate(2);
  }
</style>

in regular HTML and CSS.

.something::after selects the div after the span in the React component.

-webkit-filter is replaced with WebkitFilter in React.

Conclusion

To add CSS pseudo elements in React, we add real elements.

Categories
JavaScript Answers

How to make canvas as wide and as high as parent with JavaScript?

Sometimes, we want to make canvas as wide and as high as parent with JavaScript.

In this article, we’ll look at how to make canvas as wide and as high as parent with JavaScript.

How to make canvas as wide and as high as parent with JavaScript?

To make canvas as wide and as high as parent with JavaScript, we can set the width and height.

For instance, we write

const canvas = document.querySelector("canvas");

const fitToContainer = (canvas) => {
  canvas.style.width = "100%";
  canvas.style.height = "100%";
  canvas.width = canvas.offsetWidth;
  canvas.height = canvas.offsetHeight;
};

fitToContainer(canvas);

to define the fitToContainer to set the width and height of the canvas to fit the parent with

canvas.style.width = "100%";
canvas.style.height = "100%";
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;

Then we call fitToContainer with canvas to resize the canvas.

Conclusion

To make canvas as wide and as high as parent with JavaScript, we can set the width and height.

Categories
JavaScript Answers

How to getElementByClass instead of GetElementById with JavaScript?

Sometimes, we want to getElementByClass instead of GetElementById with JavaScript.

In this article, we’ll look at how to getElementByClass instead of GetElementById with JavaScript.

How to getElementByClass instead of GetElementById with JavaScript?

To getElementByClass instead of GetElementById with JavaScript, we use the getElementsByClassName method.

For instance, we write

document.getElementsByClassName("classname")[0].style.display = "none";

to get the first element with class classname with

document.getElementsByClassName("classname")[0]

Then we set the style.display property of it to 'none' to set its display CSS style to none.

Conclusion

To getElementByClass instead of GetElementById with JavaScript, we use the getElementsByClassName method.