Open In App

How to Filter Keys of Type string[] in TypeScript ?

Last Updated : 22 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In TypeScript, the filtering of keys of type string[] can be done by iterating over the data object, and applying the condition to the string if the key is a string. If the condition is satisfied, then an array of output results is created which consists of filtered keys of type string[]. The below methods can be used to filter the keys of type string[].

Using the for/in Loop

In this approach, we use the for/in loop to iterate over the keys of the object and check if each key is a string type if it is a string type, then we push those keys into the array and print the array as the filters list which contains keys of type string.

Syntax:

for (variable in iterable) {
// code
}

Example: The below example filters the keys of string[] in TypeScript using for/in loop.

Javascript




const data: { [key: string]: any } = {
    title: 'GeeksforGeeks',
    domains: ['Java', 'DSA', 'Web Dev'],
    author: 'GFG User',
};
 
const res: string[] = [];
for (const key in data) {
    if (data.hasOwnProperty(key) &&
        typeof data[key] === 'string') {
        res.push(key);
    }
}
 
console.log(res);


Output:

["title", "author"] 

Using the Object.keys() and filter() methods

In this approach, we use Object.keys() method to get the array of keys from the object. Then the filter method is applied to this array by keeping only the keys that are of type string. The result array consists of only the filtered keys which are of type string[].

Syntax:

const filteredKeys: string[] = Object.keys(object)
.filter(key => /* condition */);

Example: The below example filters the keys of string[] in TypeScript using Object.keys and filter methods.

Javascript




const data: { [key: string]: any } = {
    title: 'GeeksforGeeks',
    topics: ['Java', 'DSA', 'Web Dev'],
    author: 'GFG User 2',
};
 
const res: string[] =
    Object.keys(data).
        filter(key => typeof data[key] === 'string');
 
console.log(res);


Output:

["title", "author"] 

Using Object.entries() and map() methods

In this approach, we use the Object.entries() method which returns the array of key-value pairs from the object. Then by using the map method, we extract the keys from the filter’s key-value pairs by applying the filter condition. The resulting array is printed which are the keys of the type string.

Syntax:

const keyValuePairs: [string, any][] = Object.entries(object);
const mappedKeys: string[] = keyValuePairs
.filter(([_, value]) => /* condition */)
.map(([key]) => key);

Example: The below example filters the keys of string[] in TypeScript using Object.entries and map methods.

Javascript




const data: { [key: string]: any } = {
    title: 'GeeksforGeeks',
    topics: ['Java', 'DSA', 'Web Dev'],
    author: 'GFG User 3',
};
 
const res: string[] = Object.entries(data)
    .filter(([_, value]) => typeof value === 'string')
    .map(([key]) => key);
 
console.log(res);


Output:

["title", "author"] 


Like Article
Suggest improvement
Share your thoughts in the comments