Categories
JavaScript Answers

How to create multi-dimensional associative arrays in JavaScript?

Sometimes, we want to create multi-dimensional associative arrays in JavaScript.

In this article, we’ll look at how to create multi-dimensional associative arrays in JavaScript.

How to create multi-dimensional associative arrays in JavaScript?

To create multi-dimensional associative arrays in JavaScript, we can create a nested object.

For instance, we write

const myObj = {
  fred: { apples: 2, oranges: 4, bananas: 7, melons: 0 },
  mary: { apples: 0, oranges: 10, bananas: 0, melons: 0 },
  sarah: { apples: 0, oranges: 0, bananas: 0, melons: 5 },
};

console.log(myObj["fred"]["apples"]);

to create the myObj object.

In it, we add the fred, mary and sarah properties.

And we set them to objects with the apples, oranges, bananas and melons properties.

Then we get a nested property with myObj["fred"]["apples"].

Conclusion

To create multi-dimensional associative arrays in JavaScript, we can create a nested object.

Categories
JavaScript Answers

How to reload the page with hash value with JavaScript?

Sometimes, we want to reload the page with hash value with JavaScript.

In this article, we’ll look at how to reload the page with hash value with JavaScript.

How to reload the page with hash value with JavaScript?

To reload the page with hash value with JavaScript, we can set the window.location.hash property to the hash string.

Then we reload the page.

For instance, we write

window.location.hash = "#mypara";
location.reload();

to set window.location.hash to the hash string to append it to the end of the URL.

Then we call location.reload to reload the page.

Conclusion

To reload the page with hash value with JavaScript, we can set the window.location.hash property to the hash string.

Categories
HTML

How to force download of image from src=”data:image/jpeg;base64…” with HTML?

Sometimes, we want to force download of image from src="data:image/jpeg;base64…" with HTML.

In this article, we’ll look at how to force download of image from src="data:image/jpeg;base64…" with HTML.

How to force download of image from src="data:image/jpeg;base64…" with HTML?

To force download of image from src="data:image/jpeg;base64…" with HTML, we set the download attribute on the anchor element.

For instance, we write

<a download="YourFileName.jpeg" href="data:image/jpeg;base64,iVBO...CYII=">
  <img src="data:image/jpeg;base64,iVBO...CYII=" />
</a>

to set the download attribute to the name of the downloaded file.

We set href to the base64 URL of the image to download it as an image when we click on it.

Conclusion

To force download of image from src="data:image/jpeg;base64…" with HTML, we set the download attribute on the anchor element.

Categories
JavaScript Answers

How to capture the close event of popup window in JavaScript?

Sometimes, we want to capture the close event of popup window in JavaScript.

In this article, we’ll look at how to capture the close event of popup window in JavaScript.

How to capture the close event of popup window in JavaScript?

To capture the close event of popup window in JavaScript, we can watch for the window to close with a timer.

For instance, we write

const win = window.open("https://www.example.com");

const timer = setInterval(() => {
  if (win.closed) {
    clearInterval(timer);
    console.log("closed");
  }
}, 1000);

to call window.open to open a popup with the URL.

Then we call setInterval with a callback that checks if the popup is closed with win.closed every second.

If it’s true, then we call clearInterval with timer to stop the setInterval timer.

Conclusion

To capture the close event of popup window in JavaScript, we can watch for the window to close with a timer.

Categories
JavaScript Answers

How to check if token expired using this JWT library with JavaScript?

Sometimes, we want to check if token expired using this JWT library with JavaScript.

In this article, we’ll look at how to check if token expired using this JWT library with JavaScript.

How to check if token expired using this JWT library with JavaScript?

To check if token expired using this JWT library with JavaScript, we use the jwt.verify method.

For instance, we write

const jwt = require("jsonwebtoken");

const decoded = jwt.verify(token, secret);

to call jwt.verify with token and secret to verify the JWT token string against the secret string.

It’ll also check if token has expired.

Conclusion

To check if token expired using this JWT library with JavaScript, we use the jwt.verify method.