EN
TypeScript - extract string item from array
0
points
In this article, we would like to show you how to search and extract string items from an array in TypeScript.
Quick solution:
const resultArray: type = myArray.filter(element: type => element: boolean === 'string value here ...');
Practical example
In this example, we use Array.prototype.filter()
to search for 'C'
element in the given array and save the result inside resultArray
.
const myArray: string[] = ['A', 'C', 'D', 'C'];
const resultArray: string[] = myArray.filter((element: string): boolean => element === 'C');
console.log(resultArray);
Output:
[ 'C', 'C' ]