Categories
JavaScript Answers

How to access JavaScript property case-insensitively?

Sometimes, we want to access JavaScript property case-insensitively.

In this article, we’ll look at how to access JavaScript property case-insensitively.

How to access JavaScript property case-insensitively?

To access JavaScript property case-insensitively, we can use the Object.keys method to get an array of keys.

And then we use the array find method to find the key.

For instance, we write

const myObject = { mIxeDCaSEKeY: "value" };

const searchKey = "mixedCaseKey";
const asLowercase = searchKey.toLowerCase();
const k = Object.keys(myObject).find(
  (key) => key.toLowerCase() === asLowercase
);
const val = myObject[k];

to call Object.keys with myObject to get an array of keys from myObject.

Then we call find with a callback that converts key to lower case with toLowerCase.

And we compare that with asLowercase to find the key k.

Next, we get the val with myObject[key].

Conclusion

To access JavaScript property case-insensitively, we can use the Object.keys method to get an array of keys.

And then we use the array find method to find the key.

Categories
JavaScript Answers

How to get return value from setTimeout with JavaScript?

To get return value from setTimeout with JavaScript, we can create a promise from the setTimeout code.

For instance, we write

const x = () => {
  const promise = new Promise((resolve, reject) => {
    window.setTimeout(() => {
      resolve("done!");
    });
  });
  return promise;
};

const y = async () => {
  const result = await x();
  console.log(result);
};

to define function x that returns a promise created with the Promise constructor.

We call it with a callback that calls setTimeout with a callback that calls resolve to return the resolve value of the promise.

Then we return the promise.

Next, we define function y that calls x and gets the resolved value with await.

Therefore, result is 'done!'.

Categories
JavaScript Answers

How to capture tab key in text box with JavaScript?

Sometimes, we want to capture tab key in text box with JavaScript.

In this article, we’ll look at how to capture tab key in text box with JavaScript.

How to capture tab key in text box with JavaScript?

To capture tab key in text box with JavaScript, we can use the event keyCode property.

For instance, we write

document.onkeydown = (evt) => {
  const tabKey = 9;
  if (evt.keyCode === tabKey) {
    // ...
  }
};

to listen to the keydown event on the document by setting the document.onkeydown property to a function when we press a key anywhere on the page.

In the function, we check if evt.keyCode is 9.

If it is, then the tab key is pressed on the page.

Conclusion

To capture tab key in text box with JavaScript, we can use the event keyCode property.

Categories
JavaScript Answers

How to hide certain values in output from JSON.stringify() with JavaScript?

Sometimes, we want to hide certain values in output from JSON.stringify() with JavaScript.

In this article, we’ll look at how to hide certain values in output from JSON.stringify() with JavaScript.

How to hide certain values in output from JSON.stringify() with JavaScript?

To hide certain values in output from JSON.stringify() with JavaScript, we call JSON.stringify with an array of property keys that we want to include with the returned JSON string.

For instance, we write

const x = {
  x: 0,
  y: 0,
  divID: "xyz",
  privateProperty1: "foo",
  privateProperty2: "bar",
};

const str = JSON.stringify(x, ["x", "y", "divID"]);

to call JSON.stringify with object x and an array of keys from x that we want to include in the returned JSON string.

As a result, str is '{"x":0,"y":0,"divID":"xyz"}'.

Conclusion

To hide certain values in output from JSON.stringify() with JavaScript, we call JSON.stringify with an array of property keys that we want to include with the returned JSON string.

Categories
JavaScript Answers

How to get the text of a div tag using only JavaScript?

Sometimes, we want to get the text of a div tag using only JavaScript.

In this article, we’ll look at how to get the text of a div tag using only JavaScript.

How to get the text of a div tag using only JavaScript?

To get the text of a div tag using only JavaScript, we can use the textContent property.

For instance, we write

const el = document.getElementById("foo");
const textContent = el.textContent;

to select the element with getElementById.

Then we get the text of the element with the textContent property.

Conclusion

To get the text of a div tag using only JavaScript, we can use the textContent property.