Categories
JavaScript Answers

How to use getElementsByClassName() with two classes with JavaScript?

Sometimes, we want to use getElementsByClassName() with two classes with JavaScript.

In this article, we’ll look at how to use getElementsByClassName() with two classes with JavaScript.

How to use getElementsByClassName() with two classes with JavaScript?

To use getElementsByClassName() with two classes with JavaScript, we can use the querySelectorAll method.

For instance, we write

const els = document.querySelectorAll(".a,.b");

to call document.querySelectorAll to select all elements on the page with classes a and b.

We separate each class with commas in the selector.

Conclusion

To use getElementsByClassName() with two classes with JavaScript, we can use the querySelectorAll method.

Categories
JavaScript Answers

How to remove ‘&quot’ from JSON in JavaScript?

Sometimes, we want to remove ‘&quot’ from JSON in JavaScript.

In this article, we’ll look at how to remove ‘&quot’ from JSON in JavaScript.

How to remove ‘&quot’ from JSON in JavaScript?

To remove ‘&quot’ from JSON in JavaScript, we call the string replace method.

For instance, we write

const obj = JSON.parse(data.replace(/"/g, '"'));

to call data.replace with /"/g and '"' to replace all instances of '&quot' with double quotes on the data string.

Then we call JSON.parse to parse the string returned by replace into the obj object.

Conclusion

To remove ‘&quot’ from JSON in JavaScript, we call the string replace method.

Categories
React Answers

How to retrieve value from select with multiple option in React?

Sometimes, we want to retrieve value from select with multiple option in React.

in this article, we’ll look at how to retrieve value from select with multiple option in React.

How to retrieve value from select with multiple option in React?

To retrieve value from select with multiple option in React, we can get the selected options from the element’s selectedOptions property.

For instance, we write

const Comp = () => {
  const [arrayOfOptionValues, setArrayOfOptionValues] = useState();

  const onSelectChange = (e) => {
    const values = [...e.target.selectedOptions].map((opt) => opt.value);
    setArrayOfOptionValues(values);
  };

  //...

  return (
    <select multiple value={arrayOfOptionValues} onChange={handleChange}>
      <option value={1}>First option</option>
      <option value={2}>Second option</option>
      <option value={3}>Third option</option>
    </select>
  );
};

to define the arrayOfOptionValues state with the useState hook.

Then we add the onSelectChange function that gets the selected options from e.target.selectedOptions.

We then spread that into an array and call map to get the values.

Next, we call setArrayOfOptionValues to set the values as the value of arrayOfOptionValues.

Finally, we add a select element that takes multiple values and set the value prop to arrayOfOptionValues and the onChange prop to the handleChange function.

Then then selected options are set as the value of arrayOfOptionValues when they change.

Conclusion

To retrieve value from select with multiple option in React, we can get the selected options from the element’s selectedOptions property.

Categories
JavaScript Answers

How to compare arrays of objects in JavaScript?

Sometimes, we want to compare arrays of objects in JavaScript.

In this article, we’ll look at how to compare arrays of objects in JavaScript.

How to compare arrays of objects in JavaScript?

To compare arrays of objects in JavaScript, we can use some array properties and methods.

For instance, we write

const obj1 = { name: "John", age: 33 };
const obj2 = { age: 33, name: "John" };
const obj3 = { name: "John", age: 45 };

const arr1 = [obj1, obj1];
const arr2 = [obj1, obj2];
const arr3 = [obj1, obj3];

const objectsEqual = (o1, o2) =>
  typeof o1 === "object" && Object.keys(o1).length > 0
    ? Object.keys(o1).length === Object.keys(o2).length &&
      Object.keys(o1).every((p) => objectsEqual(o1[p], o2[p]))
    : o1 === o2;

const arraysEqual = (a1, a2) =>
  a1.length === a2.length && a1.every((o, idx) => objectsEqual(o, a2[idx]));

console.log(arraysEqual(arr1, arr2)); // true
console.log(arraysEqual(arr1, arr3));

to define the objectsEqual function that recursive checks if every property in objects o1 and o2 are equal.

We do this by checking if o1 is an object.

If it is, then we check if the number of properties of o1 and o2 are the same with

Object.keys(o1).length === Object.keys(o2).length

And then we use

Object.keys(o1).every((p) => objectsEqual(o1[p], o2[p]))

to check if each property is equal.

We call objectsEqual with the property p of o1 and o2 since they’re objects.

Otherwise, we compare the values directly with

o1 === o2

Then we define the arrayEqual function that checks if array a1 and a2‘s length and content are equal with

a1.length === a2.length && a1.every((o, idx) => objectsEqual(o, a2[idx]);

Conclusion

To compare arrays of objects in JavaScript, we can use some array properties and methods.

Categories
JavaScript Answers

How to replace a regex capture group with uppercase in JavaScript?

Sometimes, we want to replace a regex capture group with uppercase in JavaScript.

In this article, we’ll look at how to replace a regex capture group with uppercase in JavaScript.

How to replace a regex capture group with uppercase in JavaScript?

To replace a regex capture group with uppercase in JavaScript, we call string replace with a callback.

For instance, we write

const r = a.replace(/(f)/, (v) => {
  return v.toUpperCase();
});

to call a string’s replace to find the capturing group with f inside.

And we replace the matches values with the value converted to upper case with toUpperCase.

Conclusion

To replace a regex capture group with uppercase in JavaScript, we call string replace with a callback.