How to Check Whether an Array Contains a String in TypeScript ?
Last Updated :
01 Aug, 2024
To check whether an array contains a string in typescript we have a different approach. In this article, we are going to learn how to check whether an array contains a string in typescript.
Below are the approaches to check whether an array contains a string in typescript:
Approach 1: Using Array.includes()
method
The includes
method is a straightforward way to check if an array contains a specific value, including strings.
Syntax:
const arrayContainsString = myArray.includes("yourString");
Example: In this example, we are using Array.include() method.
JavaScript
const myArray: string[] = ["apple", "banana",
"orange"];
if (myArray.includes("banana")) {
console.log(`Array contains the
string 'banana'.`);
} else {
console.log(`Array does not contain
the string 'banana'.`);
}
Output:
Array contains the string 'banana'.
Approach 2: Using Array.indexOf()
method
The indexOf
method returns the index of the first occurrence of a specified element in an array. If the element is not present, it returns -1.
Syntax:
const index = myArray.indexOf("yourString");
const arrayContainsString = index !== -1;
Example: In this example, we are using Array.indexof() method.
JavaScript
const myArray: string[] = ["apple",
"banana", "orange"];
const searchString: string = "banana";
if (myArray.indexOf(searchString) !== -1) {
console.log(`Array contains the
string '${searchString}'.`);
} else {
console.log(`Array does not contain
the string '${searchString}'.`);
}
Output:
Array contains the string 'banana'.
Approach 3: Using Array.some()
method
The some
method tests whether at least one element in the array passes the provided function.
Syntax:
const arrayContainsString = myArray.some(item => item === "yourString");
Example: In this example, we are using Array.some() method.
JavaScript
const myArray: string[] = ["apple",
"banana", "orange"];
const searchString: string = "banana";
if (myArray.some(item => item === searchString)) {
console.log(`Array contains
the string '${searchString}'.`);
} else {
console.log(`Array does not
contain the string '${searchString}'.`);
}
Output:
Array contains the string 'banana'.
Approach 4: Using Array.find() method
The find() method returns the value of the first element in the provided array that satisfies the provided testing function. Otherwise, it returns undefined.
Syntax:
const foundString = myArray.find(item => item === "yourString");
const arrayContainsString = foundString !== undefined;
Example: In this example, we are using Array.find() method.
JavaScript
const myArray: string[] = ["apple", "banana", "orange"];
const searchString: string = "banana";
const foundString = myArray.find(item => item === searchString);
if (foundString) {
console.log(`Array contains the string '${searchString}'.`);
} else {
console.log(`Array does not contain the string '${searchString}'.`);
}
Output:
Array contains the string 'banana'.
Approach 5: Using Array.find() method with a boolean condition
The Array.find() method can be utilized with a boolean condition to check if an array contains a specific string. This approach returns the found string if it exists in the array, otherwise returns undefined.
Example: In this example, we'll use the Array.find() method with a boolean condition to determine if an array contains a string.
JavaScript
const myArray: string[] = ["apple", "banana", "orange"];
const searchString: string = "banana";
const foundString = myArray.find(item => item === searchString);
if (foundString) {
console.log(`Array contains the string '${searchString}'.`);
} else {
console.log(`Array does not contain the string '${searchString}'.`);
}
Output
Array contains the string 'banana'.
Approach 6: Using a Set for Fast Lookup
Another efficient way to check if an array contains a specific string in TypeScript is by using a Set. This approach is particularly useful for large arrays, as it provides a faster lookup time due to the nature of Set operations being O(1) on average.
Example: In this example, we convert the array to a Set and then check if the Set contains the specific string.
JavaScript
const myArray: string[] = ["apple", "banana", "cherry", "date"];
const mySet: Set<string> = new Set(myArray);
const stringToFind: string = "banana";
const arrayContainsString: boolean = mySet.has(stringToFind);
if (arrayContainsString) {
console.log(`Array contains the string '${stringToFind}'.`);
} else {
console.log(`Array does not contain the string '${stringToFind}'.`);
}
Output:
Array contains the string 'banana'.
Similar Reads
How to Declare an Array of Strings in TypeScript ?
Arrays are fundamental data structures in TypeScript, enabling developers to manage collections of elements efficiently. Below are the approaches to declare an Array of strings in TypeScript:Table of ContentSquare Brackets NotationArray ConstructorSquare Brackets NotationUsing square brackets notati
1 min read
How to Convert a Set to an Array in TypeScript ?
A Set in TypeScript is used to create a particular type of list that does not contain duplicate elements. If any element is repeated more than once it will automatically remove the duplicate existence and consider it only once in the list. In this article, we will convert these types of lists into a
5 min read
JavaScript - How To Check Whether a String Contains a Substring?
Here are the different methods to check whether a string contains a substring in JavaScript.1. Using includes() MethodThe includes() method is the most simple and modern way to check if a string contains a specific substring. It returns true if the substring is found within the string, and false oth
3 min read
How to Convert a Dictionary to an Array in TypeScript ?
In TypeScript, converting a dictionary(an object) into an Array can be useful in scenarios where you need to work with the values in a more iterable and flexible manner. These are the following approaches: Table of Content Using Object.keys methodUsing Object.values methodUsing Object.entries method
3 min read
How to use Optional Chaining with Arrays and Functions in TypeScript ?
In this article, we will learn how we can use optional chaining with arrays and functions in TypeScript. Optional chaining in Typescript is mainly a feature that allows us to safely access the properties or the call functions on potentially null or undefined values without causing any runtime errors
3 min read
How to Create an Enum With String Values in TypeScript ?
To create an enum with string values in TypesScript, we have different approaches. In this article, we are going to learn how to create an enum with string values in TypesScript. Below are the approaches used to create an enum with string values in TypesScript: Table of Content Approach 1: Using Enu
3 min read
How to Check if an Array Includes an Object in TypeScript ?
In TypeScript, checking if an array includes an object consists of comparing the object's properties within the array elements. We can compare and check this using three different approaches some method, find method, and includes method. There are several ways to check if an array includes an object
3 min read
Check If An Array of Strings Contains a Substring in JavaScript
Here are the different ways to check if an array of strings contains a substring in JavaScript1. Using includes() and filter() methodWe will create an array of strings, then use includes() and filter() methods to check whether the array elements contain a sub-string.JavaScriptconst a = ['Geeks', 'gf
4 min read
How to Create a Typed Array from an Object with Keys in TypeScript?
Creating a typed array from the keys of an object in TypeScript ensures that your application maintains type safety, particularly when interacting with object properties. Here we'll explore different approaches to achieve this using TypeScript's built-in Object methods.Below are the approaches used
3 min read
Check if an Array is Empty or not in TypeScript
In TypeScript, while performing any operation on the array, it is quite important that there should be elements or data in the array, else no operation can be done. We can check whether the array contains the elements or not by using the below-listed methods: Table of Content Using length PropertyUs
2 min read