Categories
JavaScript Answers

How to find the first matching element given an array and predicate with JavaScript?

Sometimes, we want to find the first matching element given an array and predicate with JavaScript.

In this article, we’ll look at how to find the first matching element given an array and predicate with JavaScript.

How to find the first matching element given an array and predicate with JavaScript?

The first matching element given an array and predicate with JavaScript, we can use the array find method.

For instance, we write:

const a = [2, 4, 10, 5, 7, 20]
const r = a.find(x => x % 2)
console.log(r)

to find the first element in a that is odd with find.

We call a.find with x => x % 2 to return the first element in a that isn’t evenly divisible by 2.

Therefore, r is 5.

Conclusion

The first matching element given an array and predicate with JavaScript, we can use the array find method.

Categories
JavaScript Answers

How to get localized day of week with JavaScript?

Sometimes, we want to get localized day of week with JavaScript.

In this article, we’ll look at how to get localized day of week with JavaScript.

How to get localized day of week with JavaScript?

To get localized day of week with JavaScript, we can use the date’s toLocaleString method.

For instance, we write:

const d = new Date();
const dStr = d.toLocaleString(window.navigator.language, {
  weekday: 'long'
});
console.log(dStr)

to call d.toLocaleString with the language of the device the code is running on and an object with weekday set to 'long' to return a localize string of the current date.

Conclusion

To get localized day of week with JavaScript, we can use the date’s toLocaleString method.

Categories
JavaScript Answers

How to randomly select array item without repeats with JavaScript?

Sometimes, we want to randomly select array item without repeats with JavaScript.

In this article, we’ll look at how to randomly select array item without repeats with JavaScript.

How to randomly select array item without repeats with JavaScript?

To randomly select array item without repeats with JavaScript, we can shuffle the array and then return the number of items we want from the shuffled array.

For instance, we write:

const arr = ["Roger", "Russell", "Clyde", "Egbert", "Clare", "Bobbie", "Simon", "Elizabeth", "Ted", "Caroline"];
const choices = arr.sort(() => Math.random() - 0.5).slice(0, 5)
console.log(choices);

to call arr.sort with a callback that return a number between -0.5 and 0.5 to return the shuffled version of arr.

And then we call slice with 0 and 5 to return the first 5 elements from the shuffled array.

Therefore, choices is something like ["Egbert", "Elizabeth", "Simon", "Clare", "Caroline"].

Conclusion

To randomly select array item without repeats with JavaScript, we can shuffle the array and then return the number of items we want from the shuffled array.

Categories
JavaScript Answers

How to disable all input buttons with JavaScript?

Sometimes, we want to disable all input buttons with JavaScript.

In this article, we’ll look at how to disable all input buttons with JavaScript.

How to disable all input buttons with JavaScript?

To disable all input buttons with JavaScript, we can select all the input elements and loop through them to set the disabled property of each to true.

For instance, we write:

<input type='button' value='button'>
<input type='button' value='button'>
<input type='button' value='button'>

to add a few input elements.

Then we write:

const inputs = document.getElementsByTagName("input");
for (const input of inputs) {
  input.disabled = true
}

to select all the inputs with getElementsByTagname.

Next, we loop through each input with the for-of loop.

In the loop body, we set the disabled property of each input to true to disable them.

Conclusion

To disable all input buttons with JavaScript, we can select all the input elements and loop through them to set the disabled property of each to true.

Categories
JavaScript Answers

How to return a substring from a string to the end using JavaScript?

Sometimes, we want to return a substring from a string to the end using JavaScript.

In this article, we’ll look at how to return a substring from a string to the end using JavaScript.

How to return a substring from a string to the end using JavaScript?

To return a substring from a string to the end using JavaScript, we can use some string methods.

For instance, we write:

const strIn = 'http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe';
const searchTerm = '/dashboard.php?';
const searchIndex = strIn.indexOf(searchTerm);
const strOut = strIn.substr(searchIndex + searchTerm.length);
console.log(strOut)

to get substring from strIn after '/dashboard.php?'.

To do this, we get the index of the first character of searchTerm with strIn.indexOf(searchTerm).

Then we call strIn.substr with searchIndex + searchTerm.length to return a substring from strIn starting from index searchIndex + searchTerm.length + 1 to the end of the string.

Therefore, strOut is 'page_id=projeto_lista&lista_tipo=equipe '.

Conclusion

To return a substring from a string to the end using JavaScript, we can use some string methods.