Categories
JavaScript Answers

How to replace matched item with lodash and JavaScript?

Sometimes, we want to replace matched item with lodash and JavaScript.

In this article, we’ll look at how to replace matched item with lodash and JavaScript.

How to replace matched item with lodash and JavaScript?

To replace matched item with lodash and JavaScript, w can use the map function.

For instance, we write

const arr = [
  { id: 1, name: "Person 1" },
  { id: 2, name: "Person 2" },
];

const newArr = _.map(arr, (a) => {
  return a.id === 1 ? { id: 1, name: "Person New Name" } : a;
});

to call Lodash map with the arr array and a callback that maps the entries in arr to new values.

If the id of the object in arr is 1, then we return a new object.

Otherwise, we return a as is.

Conclusion

To replace matched item with lodash and JavaScript, w can use the map function.

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.