How to check if an array contains a value in TypeScript ? In this article, we use 4 solutions include() function, some() function, find() function ,… Please follow the article details and choose the method that best suits you.
To Check If An Array Contains A Value In Typescript
Method 1: Use include()
function
We initialize an array containing both numbers and strings. And use function include()
to check whether the array contains a certain element. If the element exists in the array, the compiler will return “true” and vice versa.
Syntax:
include(searchElement)
Parameters:
searchElement
: the value to search for.
Input:
let number = [1, 2, 3, "four", "five", "six"]; console.log(number); console.log(number.includes('four'));
Output:
[ 1, 2, 3, 'four', 'five', 'six' ] true
Method 2: Check the index of element by function indexOf
()
We still have an array like method 1, but we will use the indexOf
() function. The compiler will return the index of elements in the array.
Syntax:
array.indexOf(searchElement[, fromIndex])
Parameters:
searchElement
: the value to search for.fromIndex
: this parameter is the index to begin the search.
Input:
let number = [1, 2, 3, "four", "five", "six"]; console.log(number); console.log(number.indexOf(3));
Output:
[ 1, 2, 3, 'four', 'five', 'six' ] 2
If the element does not exist in the array, it will return -1
Input:
let number = [1, 2, 3, "four", "five", "six"]; console.log(number); console.log(number.indexOf(44));
Output:
[ 1, 2, 3, 'four', 'five', 'six' ] -1
Method 3: Use a function array. some()
For arrays containing objects, the include function will not work.
We have to use a function array. some()
. The some() method checks if some element in the array passes the check performed by the provided function.
Syntax:
array.some(Object);
Parameters:
Object
: The object wants to check.
Input:
let students = [ { name: "JOHN", gender: "male", grade: 12 }, { name: "DIONA", gender: "female", grade: 11 }, { name: "Wick", gender: "male", grade: 10 }, ]; console.log(students); const result = students.some((obj) => { return obj.name === "JOHN"; }); console.log(result);
Output:
[ { name: 'JOHN', gender: 'male', grade: 12 }, { name: 'DIONA', gender: 'female', grade: 11 }, { name: 'Wick', gender: 'male', grade: 10 } ] true
In this case, JOHN already exists in an array so the compiler returns true.
Method 4: Use a function array. find()
We have to use a function array. find().
The find()
method call each element in the array until the array ends.
Syntax:
array.find(Object);
Parameters:
Object
: The object wants to check.
Input:
let students = [ { name: "JOHN", gender: "male", grade: 12 }, { name: "DIONA", gender: "female", grade: 11 }, { name: "Wick", gender: "male", grade: 10 }, ]; console.log(students); const result = students.find((obj) => { return obj.name === "JOHN"; }); console.log(result);
Output:
[ { name: 'JOHN', gender: 'male', grade: 12 }, { name: 'DIONA', gender: 'female', grade: 11 }, { name: 'Wick', gender: 'male', grade: 10 } ] { name: 'JOHN', gender: 'male', grade: 12 }
Summary
In this tutorial, we showed you how to check if an array contains a value in TypeScript. You can learn a lot of ways to solve this problem. All methods are very effective. Take care!
Leave a Reply