Categories
JavaScript Answers

How to get character value from key code in JavaScript then trim?

Sometimes, we want to get character value from key code in JavaScript then trim.

In this article, we’ll look at how to get character value from key code in JavaScript then trim.

How to get character value from key code in JavaScript then trim?

To get character value from key code in JavaScript then trim, we can use the string fromCharCode method.

For instance, we write

document.onkeydown = (e) => {
  let keyCode = e.keyCode;
  let chrCode = keyCode - 48 * Math.floor(keyCode / 48);
  let chr = String.fromCharCode(96 <= keyCode ? chrCode : keyCode);
  console.log(chr);
};

to get the keyCode from the keydown event object e to get the key pressed.

Then we get the chrCode character code from the keyCode with keyCode - 48 * Math.floor(keyCode / 48).

Then we get the character from charCode with String.fromCharCode.

Conclusion

To get character value from key code in JavaScript then trim, we can use the string fromCharCode method.

Categories
JavaScript Answers

How to convert array of objects into one object in JavaScript?

Sometimes, we want to convert array of objects into one object in JavaScript.

In this article, we’ll look at how to convert array of objects into one object in JavaScript.

How to convert array of objects into one object in JavaScript?

To convert array of objects into one object in JavaScript, we can use the array reduce method.

For instance, we write

const arr = [
  { key: "11", value: "1100" },
  { key: "22", value: "2200" },
];
const object = arr.reduce(
  (obj, item) => Object.assign(obj, { [item.key]: item.value }),
  {}
);

console.log(object);

to call arr.reduce with a callback that calls Object.assign with obj and an object with the property that we want to add to obj and return it.

The 2nd argument is an empty object so obj‘s initial value is an empty object.

Conclusion

To convert array of objects into one object in JavaScript, we can use the array reduce method.

Categories
JavaScript Answers

How to add property to an array of objects with JavaScript?

Sometimes, we want to add property to an array of objects with JavaScript.

In this article, we’ll look at how to add property to an array of objects with JavaScript.

How to add property to an array of objects with JavaScript?

To add property to an array of objects with JavaScript, we can use the array map method.

For instance, we write

const newResults = results.map((obj) => ({ ...obj, active: "false" }));

to call results.map with a callback that returns the object we want to return by copying the existing object obj and add the active property to it.

Then we assign the array of mapped objects returned to newResults.

Conclusion

To add property to an array of objects with JavaScript, we can use the array map method.

Categories
JavaScript Answers

How to get minimum and maximum date in JavaScript?

Sometimes, we want to get minimum and maximum date in JavaScript.

In this article, we’ll look at how to get minimum and maximum date in JavaScript.

How to get minimum and maximum date in JavaScript?

To get minimum and maximum date in JavaScript, the can use the timestamps for the min and max date in milliseconds.

For instance, we write

let maxDate = new Date(8640000000000000);
let minDate = new Date(-8640000000000000);

to use 8640000000000000 milliseconds since epoch as the max date timestamp.

Then we can pass that to the Date constructor to create a date object.

Likewise, we use -8640000000000000 milliseconds since epoch as the max date timestamp.

And then we create the Date object from it the same way.

Conclusion

To get minimum and maximum date in JavaScript, the can use the timestamps for the min and max date in milliseconds.

Categories
JavaScript Answers

How to clear this setInterval inside a function with JavaScript?

Sometimes, we want to clear this setInterval inside a function with JavaScript.

In this article, we’ll look at how to clear this setInterval inside a function with JavaScript.

How to clear this setInterval inside a function with JavaScript?

To clear this setInterval inside a function with JavaScript, we can call the clearInterval function.

For instance, we write

let intervalListener;
const someProcess = () => {
  //...
  if (stopIntervalIsTrue) {
    window.clearInterval(intervalListener);
  }
};
intervalListener = self.setInterval(someProcess, 1000);

to call setInterval with someProcess to run someProcess every second.

Then if stopIntervalIsTrue is true in someProcess to call window.clearInterval with intervalListener to clear the timer and stop someProcess from running.

Conclusion

To clear this setInterval inside a function with JavaScript, we can call the clearInterval function.