In this chapter, we will explore the every()
method for arrays in TypeScript. This method is a built-in function that tests whether all elements in the array pass the test implemented by the provided function. Understanding how to use every()
is useful for performing validations and checks on arrays.
Table of Contents
- Definition
- Syntax
- Examples
- Conclusion
1. Definition
The every()
method tests whether all elements in the array pass the test implemented by the provided function. It returns a boolean value indicating whether all elements in the array satisfy the condition.
2. Syntax
array.every(callback(element, index, array), thisArg?);
Parameters
callback
: A function to test for each element, taking three arguments:element
: The current element being processed in the array.index
(optional): The index of the current element being processed in the array.array
(optional): The arrayevery
was called upon.
thisArg
(optional): Value to use asthis
when executingcallback
.
Return Value
The method returns true
if the callback
function returns a truthy value for every array element; otherwise, it returns false
.
3. Examples
Let's look at some examples to understand how every()
works in TypeScript.
Example 1: Basic Usage
In this example, we check if all elements in the array are greater than 10.
let numbers: number[] = [12, 14, 16, 18];
let result = numbers.every((num) => num > 10);
console.log(result); // Output: true
Example 2: Check for Even Numbers
In this example, we check if all elements in the array are even.
let numbers: number[] = [2, 4, 6, 8];
let result = numbers.every((num) => num % 2 === 0);
console.log(result); // Output: true
Example 3: Using Index and Array in Callback
In this example, we use the index and the array itself within the callback function.
let numbers: number[] = [1, 3, 5, 7];
let result = numbers.every((num, index, arr) => {
console.log(`Element: ${num}, Index: ${index}, Array: ${arr}`);
return num % 2 !== 0;
});
console.log(result); // Output: true
// Console will also log each element, index, and array.
Example 4: Empty Array
In this example, we check the behavior of every()
on an empty array.
let numbers: number[] = [];
let result = numbers.every((num) => num > 0);
console.log(result); // Output: true
Example 5: Array of Objects
In this example, we check if all objects in an array have a specific property value.
interface Person {
name: string;
age: number;
}
let people: Person[] = [
{ name: "Ravi", age: 25 },
{ name: "Ankit", age: 30 },
{ name: "Priya", age: 28 },
];
let result = people.every((person) => person.age > 20);
console.log(result); // Output: true
4. Conclusion
In this chapter, we explored the every()
method for arrays in TypeScript, which is used to test whether all elements in the array pass the test implemented by the provided function. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use every()
effectively can help in various array validation tasks in TypeScript, especially when checking if all elements in an array meet certain conditions.
Comments
Post a Comment
Leave Comment