Array.every()

.every()

The every() method return true/false based on whether or not every element in the source array passes a certain condition or not

You’ll exit on the first failure. If the condition returns false for something, it stop processing the array there and returns.

Examples

// EXAMPLE 1
const items = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
const result = items.every(x => x < 10) // check every item is <10?
const result2 = items.every(x => x > 5) // check every item is >5?

console.info('result:', result) // result: true
console.info('result2:', result2) // result2: false

Check if an array contains only strings

// EXAMPLE 2
const items2 = [1, 4, 7, 'hello', 'blah']
const strings = items2.every(x => typeof x === 'string') // check if every items is a string?
console.info('strings:', strings) // strings: false

Check if a form submission was valid

// EXAMPLE 3
const fields = [
	{
		field: 'email',
		value: 'shane@email.com',
		errors: []
	},
	{
		field: 'name',
		value: '',
		errors: ['No name provided']
	}
]

const isValid = fields.every(x => x.errors.length === 0 ) // check if no errors?
console.info('isValid:', isValid) // isValid: false

Check if a user has finished watching the course

// EXAMPLE 4
const videos = [
	{
		title: 'Array methods in depth: concat',
		length: 310, // video length in secs
		viewed: 310	// amount of secs watched
	},
	{
		title: 'Array methods in depth: join',
		length: 420,
		viewed: 360
	}
]
const isComplete = videos.every(x => x.viewed === x.length)
console.info('isComplete:', isComplete) // isComplete: false