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:
xxxxxxxxxx
1
const resultArray: type = myArray.filter(element: type => element: boolean === 'string value here ...');
In this example, we use Array.prototype.filter()
to search for 'C'
element in the given array and save the result inside resultArray
.
xxxxxxxxxx
1
const myArray: string[] = ['A', 'C', 'D', 'C'];
2
const resultArray: string[] = myArray.filter((element: string): boolean => element === 'C');
3
4
console.log(resultArray);
Output:
xxxxxxxxxx
1
[ 'C', 'C' ]