Languages
[Edit]
EN

TypeScript - extract string item from array

0 points
Created by:
Richard-Bevan
413

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' ]

References

  1. JavaScript - extract string item from array
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join