Categories
JavaScript Answers

How to add a hash table in JavaScript?

To add a hash table in JavaScript, we create an object literal.

For instance, we write

const myHash = {};
myHash["one"] = [1, 10, 5];
myHash["two"] = [2];
myHash["three"] = [3, 30, 300];

to define the myHash object.

And then add keys and set them to values.

'one', 'two' and 'three' are the keys and the arrays are their values.

Categories
JavaScript Answers

How to add img element onclick event handler with JavaScript?

Sometimes, we want to add img element onclick event handler with JavaScript.

In this article, we’ll look at how to add img element onclick event handler with JavaScript.

How to add img element onclick event handler with JavaScript?

To add img element onclick event handler with JavaScript, we set the onclick attribute to the JavaScript code we want to run.

For instance, we write

<img src="https://picsum.photos/30/30" onclick="window.open(this.src)" />

to set the onclick attribute to window.open(this.src) to call window.open with the img element’s src attribute to open the image in a new window when we click the image.

Conclusion

To add img element onclick event handler with JavaScript, we set the onclick attribute to the JavaScript code we want to run.

Categories
HTML

How to fix &times word in HTML changes to ×?

Sometimes, we want to fix &times word in HTML changes to ×.

In this article, we’ll look at how to fix &times word in HTML changes to ×.

How to fix &times word in HTML changes to ×?

To fix &times word in HTML changes to ×, we should escape the ampersand.

For instance, we write

<div class="test">&amp;times</div>

to add &amp; to render an ampersand on the page.

Conclusion

To fix &times word in HTML changes to ×, we should escape the ampersand.

Categories
JavaScript Answers

How to count the number of times a same value appears in a JavaScript array?

Sometimes, we want to count the number of times a same value appears in a JavaScript array.

In this article, we’ll look at how to count the number of times a same value appears in a JavaScript array.

How to count the number of times a same value appears in a JavaScript array?

To count the number of times a same value appears in a JavaScript array, we use the array filter method.

For instance, we write

const getOccurrence = (array, value) => {
  return array.filter((v) => v === value).length;
};

const arr = [2, 3, 1, 3, 4, 5, 3, 1];
console.log(getOccurrence(arr, 1));

to define the getOccurrence function with the array and value as parameters.

And we return the number of times value appears in array by calling “array.filterwith a callback that returns the predicatevinarrayequalsvalueand then get the returned array'slength`.

Conclusion

To count the number of times a same value appears in a JavaScript array, we use the array filter method.

Categories
JavaScript Answers

How to load an image from URL into buffer in Node.js and JavaScript?

Sometimes, we want to load an image from URL into buffer in Node.js and JavaScript.

In this article, we’ll look at how to load an image from URL into buffer in Node.js and JavaScript.

How to load an image from URL into buffer in Node.js and JavaScript?

To load an image from URL into buffer in Node.js and JavaScript, we use Axios.

For instance, we write

const response = await axios.get(url, { responseType: "arraybuffer" });
const buffer = Buffer.from(response.data, "utf-8");

to make a get request to the image url with axios.get.

We get the response as an array buffer by setting the responseType to 'arraybuffer'.

And then we call Buffer.from to convert the response.data response we get from the promise returned by get body to a buffer.

Conclusion

To load an image from URL into buffer in Node.js and JavaScript, we use Axios.