Categories
JavaScript Answers

How to do integer division in JavaScript?

Sometimes, we want to do integer division in JavaScript.

In this article, we’ll look at how to do integer division in JavaScript.

How to do integer division in JavaScript?

To do integer division in JavaScript, we get the floor of the quotient with Math.floor.

For instance, we write

const answer = Math.floor(x);

to get the floor of quotient x with Math.floor and assign it to answer.

Conclusion

To do integer division in JavaScript, we get the floor of the quotient with Math.floor.

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.