Checking if every item an array matches some criteria with the vanilla JS Array.prototype.every() method

Today, we’re going to learn how to check if every item an array matches some criteria. 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.


This content originally appeared on Go Make Things and was authored by Go Make Things

Today, we’re going to learn how to check if every item an array matches some criteria. 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.every() method

The Array.prototype.every() 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.every(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 all items return true, the Array.prototype.every() method returns true. Otherwise, it returns false.

Example 1: check if every wizard uses a staff

For example, let’s imagine that we want to check if every wizard in the array uses a staff as their tool.

We could do this…

// returns false
// (Merlin uses a wand)
let hasStaff = wizards.every(function (wizard) {
	return wizard.tool === 'staff';
});

Example 2: check if every 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.every(function (wizard) {
	return wizard.spells.length > 0;
});

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


Print Share Comment Cite Upload Translate Updates
APA

Go Make Things | Sciencx (2023-02-07T14:30:00+00:00) Checking if every item an array matches some criteria with the vanilla JS Array.prototype.every() method. Retrieved from https://www.scien.cx/2023/02/07/checking-if-every-item-an-array-matches-some-criteria-with-the-vanilla-js-array-prototype-every-method/

MLA
" » Checking if every item an array matches some criteria with the vanilla JS Array.prototype.every() method." Go Make Things | Sciencx - Tuesday February 7, 2023, https://www.scien.cx/2023/02/07/checking-if-every-item-an-array-matches-some-criteria-with-the-vanilla-js-array-prototype-every-method/
HARVARD
Go Make Things | Sciencx Tuesday February 7, 2023 » Checking if every item an array matches some criteria with the vanilla JS Array.prototype.every() method., viewed ,<https://www.scien.cx/2023/02/07/checking-if-every-item-an-array-matches-some-criteria-with-the-vanilla-js-array-prototype-every-method/>
VANCOUVER
Go Make Things | Sciencx - » Checking if every item an array matches some criteria with the vanilla JS Array.prototype.every() method. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/07/checking-if-every-item-an-array-matches-some-criteria-with-the-vanilla-js-array-prototype-every-method/
CHICAGO
" » Checking if every item an array matches some criteria with the vanilla JS Array.prototype.every() method." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2023/02/07/checking-if-every-item-an-array-matches-some-criteria-with-the-vanilla-js-array-prototype-every-method/
IEEE
" » Checking if every item an array matches some criteria with the vanilla JS Array.prototype.every() method." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2023/02/07/checking-if-every-item-an-array-matches-some-criteria-with-the-vanilla-js-array-prototype-every-method/. [Accessed: ]
rf:citation
» Checking if every item an array matches some criteria with the vanilla JS Array.prototype.every() method | Go Make Things | Sciencx | https://www.scien.cx/2023/02/07/checking-if-every-item-an-array-matches-some-criteria-with-the-vanilla-js-array-prototype-every-method/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.