If you have an array consisting of only strings, the following functions can be used to perform the required actions:
// Function to find if any string in the array contains a specific substring.
function checkStringInArray(array: string[], str: string)
{
let filteredArray = array.filter(x => x.includes(str));
return filteredArray.length > 0;
}
// Function to find if any string in the array starts with a particular string.
function checkStringStartsWith(array: string[], str: string)
{
let filteredArray = array.filter(x => x.startsWith(str));
return filteredArray.length > 0;
}
Example program for testing:
let testStr: string = "air";
let exampleArr1 = ["corsair","stenographer"];
let exampleArr2 = ["hey","no"];
function checkStringInArray(array: string[], str: string)
{
let filteredArray = array.filter(x => x.includes(str));
return filteredArray.length > 0;
}
function checkStringStartsWith(array: string[], str: string)
{
let filteredArray = array.filter(x => x.startsWith(str));
return filteredArray.length > 0;
}
console.log(checkStringInArray(exampleArr1, testStr));
console.log(checkStringInArray(exampleArr2, testStr));
console.log(checkStringStartsWith(exampleArr1, testStr));
console.log(checkStringStartsWith(exampleArr2, testStr));
Output:
true
false
false
false