This content originally appeared on Go Make Things and was authored by Go Make Things
Yesterday, we learned how to test if every item in an array matches some criteria. Today, we’re going to learn how to check if at least one item does instead.
Let’s dig in!
A sample array
Let’s imagine that we have an array of wizards
. Each item is an object with a name
, spells
, and tool
property.
let wizards = [
{
name: 'Radagast',
spells: ['Talk to animals', 'Grow plants'],
tool: 'staff'
},
{
name: 'Merlin',
spells: ['Dancing teacups', 'Turn into fish'],
tool: 'wand'
},
{
name: 'Gandalf',
spells: ['You shall not pass', 'Disappear'],
tool: 'staff'
}
];
The Array.prototype.some()
method
The Array.prototype.some()
method accepts a callback function as an argument.
That callback function accepts three arguments: a variable for the current
item in the loop, it’s index
, and the array
itself. All three are optional.
wizards.some(function (wizard, index, arr) {
// do stuff...
});
Inside the callback function, you can test each item in the array, and return
a boolean value (truth or falsy). If at least one item return true
, the Array.prototype.some()
method returns true
. Otherwise, it returns false
.
Let’s look at the same examples we looked at yesterday to show the difference between Array.prototype.every()
and Array.prototype.some()
.
Example 1: check if at least one wizard uses a staff
Let’s imagine that we want to check if at least one wizard in the array uses a staff as their tool
.
We could do this…
// returns true
// (Both Radagast and Gandlf use staffs)
let hasStaff = wizards.some(function (wizard) {
return wizard.tool === 'staff';
});
Example 2: check if at least one wizard has at least one spell
We could also use the Array.prototype.every()
method to check if every wizard in the array has at least one spell.
// returns true
let hasSpells = wizards.some(function (wizard) {
return wizard.spells.length > 0;
});
A more complex example: checking for a spell
Let’s imagine that you want to make sure at least one wizard in your array can cast the spell “You shall not pass”.
Inside the callback function, we could use the Array.prototype.includes()
method on the spells
array to look for our spell.
// returns true
let doNotPass = wizards.some(function (wizard) {
return wizard.spells.includes('You shall not pass');
});
Level-up your team’s core JavaScript skills. I offer project-based courses and workshops designed to fit around your team's schedule. It's perfect for remote and distributed teams.
This content originally appeared on Go Make Things and was authored by Go Make Things
Go Make Things | Sciencx (2023-02-08T14:30:00+00:00) Checking if at least one item in an array matches some criteria with the vanilla JS Array.prototype.some() method. Retrieved from https://www.scien.cx/2023/02/08/checking-if-at-least-one-item-in-an-array-matches-some-criteria-with-the-vanilla-js-array-prototype-some-method/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.