Categories
JavaScript Answers

How to call Google maps API V3 fitBounds method with LatLngBounds with JavaScript?

Sometimes, we want to call Google maps API V3 fitBounds method with LatLngBounds with JavaScript.

In this article, we’ll look at how to call Google maps API V3 fitBounds method with LatLngBounds with JavaScript.

How to call Google maps API V3 fitBounds method with LatLngBounds with JavaScript?

To call Google maps API V3 fitBounds method with LatLngBounds with JavaScript, we call extend with the SW and NE points.

For instance, we write

const bounds = new google.maps.LatLngBounds();
bounds.extend(myPlace);
bounds.extend(item1);
map.fitBounds(bounds);

to create the LatLngBounds bounds object.

Then we call extend with the myPlace and item1 points object with the SW and NE bounds.

And then we call map.fitBounds with the bounds.

Conclusion

To call Google maps API V3 fitBounds method with LatLngBounds with JavaScript, we call extend with the SW and NE points.

Categories
JavaScript Answers

How to replace nbsp; from JavaScript DOM text node?

Sometimes, we want to replace from JavaScript DOM text node.

In this article, we’ll look at how to replace from JavaScript DOM text node.

How to replace from JavaScript DOM text node?

To replace from JavaScript DOM text node, we can use the string replace method.

For instance, we write

textNode.nodeValue = textNode.nodeValue.replace(/\u00a0/g, " ");

to call nodeValue.replace with /\u00a0/g and ' ' to replace all   characters with spaces in our text node.

Conclusion

To replace from JavaScript DOM text node, we can use the string replace method.

Categories
JavaScript Answers

How to easily create empty matrices with JavaScript?

Sometimes, we want to easily create empty matrices with JavaScript.

In this article, we’ll look at how to easily create empty matrices with JavaScript.

How to easily create empty matrices with JavaScript?

To easily create empty matrices with JavaScript, we can use the array fill method.

For instance, we write

const matrix = Array(9).fill(Array(9));

to create an array with length 9 with the Array function.

Then we call fill with another empty array with 9 empty entries.

As a result, we get a 9×9 2d array returned.

Conclusion

To easily create empty matrices with JavaScript, we can use the array fill method.

Categories
JavaScript Answers

How to toggle show or hide div with button with JavaScript?

Sometimes, we want to toggle show or hide div with button with JavaScript.

In this article, we’ll look at how to toggle show or hide div with button with JavaScript.

How to toggle show or hide div with button with JavaScript?

To toggle show or hide div with button with JavaScript, we can set the style property of the element we’re showing or hiding when we click on a button.

For instance, we write

const button = document.getElementById("button");

button.onclick = () => {
  const div = document.getElementById("newpost");
  if (div.style.display !== "none") {
    div.style.display = "none";
  } else {
    div.style.display = "block";
  }
};

to select the button with getElementById.

Then set set button.onclick to a function that runs when we click on button.

In it, we select the element we want to toggle show or hide with getElementById.

If its display CSS property isn’t 'none', then we set it to 'none'.

Otherwise, we set it to 'block'.

Conclusion

To toggle show or hide div with button with JavaScript, we can set the style property of the element we’re showing or hiding when we click on a button.

Categories
JavaScript Answers

How to omit specific properties from an object in JavaScript?

Sometimes, we want to omit specific properties from an object in JavaScript.

In this article, we’ll look at how to omit specific properties from an object in JavaScript.

How to omit specific properties from an object in JavaScript?

To omit specific properties from an object in JavaScript, we can create a function that returns an object with the properties we want to keep.

For instance, we write

const exampleFilter = ({ keepMe, keepMeToo }) => ({ keepMe, keepMeToo });

console.log(
  exampleFilter({
    keepMe: "keepMe",
    keepMeToo: "keepMeToo",
    omitMe: "omitMe",
    omitMeToo: "omitMeToo",
  })
);

to define the exampleFilter function.

In it, we destructure the properties from the parameter object.

And then we return an object with the properties we destructured included,

Conclusion

To omit specific properties from an object in JavaScript, we can create a function that returns an object with the properties we want to keep.