Categories
JavaScript Answers

How to use array push() to insert unique items with JavaScript?

Sometimes, we want to use array push() to insert unique items with JavaScript.

In this article, we’ll look at how to use array push() to insert unique items with JavaScript.

How to use array push() to insert unique items with JavaScript?

To use array push() to insert unique items with JavaScript, we can use a set.

For instance, we write

const items = new Set();
items.add(item);
console.log([...items]);

to create a new set with the Set constructor.

Then we call add to add a new item to the set.

Sets can’t have duplicate items, so only the last one with the same value is kept.

And then we can use the spread operator to convert items back to an array.

Conclusion

To use array push() to insert unique items with JavaScript, we can use a set.

Categories
JavaScript Answers

How to concatenate two JSON objects with JavaScript?

Sometimes, we want to concatenate two JSON objects with JavaScript.

In this article, we’ll look at how to concatenate two JSON objects with JavaScript.

How to concatenate two JSON objects with JavaScript?

To concatenate two JSON objects with JavaScript, we can use the object spread operator.

For instance, we write

const obj = { ...sourceObj1, ...sourceObj2 };

to put the properties of sourceObj1 and sourceObj2 into the new object after making a shallow copy of the 2 objects.

Then we assign the new object to obj.

Conclusion

To concatenate two JSON objects with JavaScript, we can use the object spread operator.

Categories
JavaScript Answers

How to set the focus to the first input element in an HTML form independent from the ID with JavaScript?

Sometimes, we want to set the focus to the first input element in an HTML form independent from the ID with JavaScript.

In this article, we’ll look at how to set the focus to the first input element in an HTML form independent from the ID with JavaScript.

How to set the focus to the first input element in an HTML form independent from the ID with JavaScript?

To set the focus to the first input element in an HTML form independent from the ID with JavaScript, we can use the document.forms property to get the form and the inputs inside the form.

For instance, we write

document.forms[0].elements[0].focus();

to get the first form with document.forms[0].

Then we use elements[0] to get the first element in the form.

Finally, we call focus on it to focus on the element.

Conclusion

To set the focus to the first input element in an HTML form independent from the ID with JavaScript, we can use the document.forms property to get the form and the inputs inside the form.

Categories
JavaScript Answers

How to insert a character after every n characters in JavaScript?

Sometimes, we want to insert a character after every n characters in JavaScript.

In this article, we’ll look at how to insert a character after every n characters in JavaScript.

How to insert a character after every n characters in JavaScript?

To insert a character after every n characters in JavaScript, we can use the string’s match and array join methods.

For instance, we write

const str = "123456789";
const parts = str.match(/.{1,3}/g);
const newValue = parts.join("-");

to call str.match with a regex that matches all groups of 3 digits and return the matches in an array.

Then we call join with '-' to join the matches text with '-'.

Conclusion

To insert a character after every n characters in JavaScript, we can use the string’s match and array join methods.

Categories
JavaScript Answers

How to call removeEventListener with an anonymous function with JavaScript?

Sometimes, we want to call removeEventListener with an anonymous function with JavaScript.

In this article, we’ll look at how to call removeEventListener with an anonymous function with JavaScript.

How to call removeEventListener with an anonymous function with JavaScript?

To call removeEventListener with an anonymous function with JavaScript, we assign the anonymous event listener function to a variable and call addEventListener and removeEventListener with the variable.

For instance, we write

const onScroll = () => {
  //...
};
document.body.addEventListener("scroll", onScroll, false);

setTimeout(() => {
  document.body.removeEventListener("scroll", onScroll, false);
}, 3000);

to call addEventListener to add the onScroll function as the scroll body element’s scroll event handler.

Then we call removeEventListener in the setTimeout callback that to remove the onScroll function as the body element’s scroll event handler.

Conclusion

To call removeEventListener with an anonymous function with JavaScript, we assign the anonymous event listener function to a variable and call addEventListener and removeEventListener with the variable.