Categories
JavaScript Answers

How to remove all session storage items with keys that match a certain pattern with JavaScript?

Sometimes, we want to remove all session storage items with keys that match a certain pattern with JavaScript.

In this article, we’ll look at how to remove all session storage items with keys that match a certain pattern with JavaScript.

How to remove all session storage items with keys that match a certain pattern with JavaScript?

To remove all session storage items with keys that match a certain pattern with JavaScript, we can loop through the keys of the items we want to remove and remove the entries with the keys.

For instance, we write:

const keys = Object.keys(sessionStorage)
  .filter((k) => {
    return /foo/.test(k);
  })

for (const k of keys) {
  sessionStorage.removeItem(k);
}

to get the keys of the items we want to remove with:

const keys = Object.keys(sessionStorage)
  .filter((k) => {
    return /foo/.test(k);
  })

Then we loop through the keys with a for-of loop.

In it, we call sessionStorage.removeItem with key k to remove the entry with k as the key.

Conclusion

To remove all session storage items with keys that match a certain pattern with JavaScript, we can loop through the keys of the items we want to remove and remove the entries with the keys.

Categories
JavaScript Answers

How to get the mouse click coordinates for image with JavaScript?

Sometimes, we want to get the mouse click coordinates for image with JavaScript.

In this article, we’ll look at how to get the mouse click coordinates for image with JavaScript.

How to get the mouse click coordinates for image with JavaScript?

To get the mouse click coordinates for image with JavaScript, we can add a click event listener to the img element.

For instance, we write:

<img src="https://picsum.photos/200/300">

to add an img element.

Then we write:

const img = document.querySelector("img")
img.onclick = (e) => {
  const x = e.pageX - e.target.offsetLeft;
  const y = e.pageY - e.target.offsetTop;
  console.log(x, y);
}

to select the img element with querySelector.

Then we set img.onclick to a function that gets the x and y coordinates of the mouse click in the page with e.pageX - e.target.offsetLeft and e.pageY - e.target.offsetTop.

Conclusion

To get the mouse click coordinates for image with JavaScript, we can add a click event listener to the img element.

Categories
JavaScript Answers

How to change textbox border color using JavaScript?

Sometimes, we want to change textbox border color using JavaScript.

In this article, we’ll look at how to change textbox border color using JavaScript.

How to change textbox border color using JavaScript?

To change textbox border color using JavaScript, we can set the style.border property of the input to red.

For instance, we write:

<input type="text" />

to add an input.

Then we write:

document.querySelector("input").style.border = "1px solid red";

to select the input with querySelector.

And we set the style.border property of it to '1px solider red' to add a red border to it.

Conclusion

To change textbox border color using JavaScript, we can set the style.border property of the input to red.

Categories
JavaScript Answers

How to merge 2 JavaScript objects, populating the properties in one if they don’t exist in the other?

Sometimes, we want to merge 2 JavaScript objects, populating the properties in one if they don’t exist in the other.

In this article, we’ll look at how to merge 2 JavaScript objects, populating the properties in one if they don’t exist in the other.

How to merge 2 JavaScript objects, populating the properties in one if they don’t exist in the other?

To merge 2 JavaScript objects, populating the properties in one if they don’t exist in the other, we can use some object methods.

For instance, we write:

const a = {
  'foo': 'bar',
  'baz': 'bat'
};
const b = {
  'foo': 'quux'
};
const entries = Object.entries(a).filter(([key]) => Object.keys(a).includes(key))

const filteredA = Object.fromEntries(entries)
const c = {
  ...b,
  ...filteredA
}
console.log(c)

to make a copy of b and merge the properties that aren’t in b into a and assign the merged object to c.

To do this, we call Object.enttries with a to return a nested array with all the key-value pairs.

Then we call filter on the returned array with a callback that returns an array of key-value pairs that are only in a.

Next, we call Object.fromEntries to converted the nested key-value pair array back to an object.

Finally, we merge b and filteredA into c.

Therefore, c is:

{
  baz: "bat",
  foo: "bar"
}

Conclusion

To merge 2 JavaScript objects, populating the properties in one if they don’t exist in the other, we can use some object methods.

Categories
JavaScript Answers

How to raise 10 to a power in JavaScript?

Sometimes, we want to raise 10 to a power in JavaScript.

In this article, we’ll look at how to raise 10 to a power in JavaScript.

How to raise 10 to a power in JavaScript?

To raise 10 to a power in JavaScript, we can use the exponentiation (**) operator.

For instance, we write:

const precision = 5
const result = 10 ** precision;
console.log(result)

to raise 10 the precision power.

Therefore, result is 100000.

Conclusion

To raise 10 to a power in JavaScript, we can use the exponentiation (**) operator.