Open In App

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

Last Updated : 29 Apr, 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"] 

Using Object.getOwnPropertyNames()

We can use Object.getOwnPropertyNames() to obtain an array of all properties (including non-enumerable properties) found directly in a given object, and then use Array.prototype.filter() to filter out keys of type string[].

Syntax:

Object.getOwnPropertyNames(obj);

Example: In this example the filters keys of type string[] from an object data using Object.getOwnPropertyNames(), checking if each value is of type ‘string’, then logs the resulting keys.

JavaScript
const data: { [key: string]: any } = {
    title: 'GeeksforGeeks',
    topics: ['Java', 'DSA', 'Web Dev'],
    author: 'GFG User 4',
};

const stringKeys: string[] = Object.getOwnPropertyNames(data)
    .filter(key => typeof data[key] === 'string');

console.log(stringKeys);

Output:

["title", "author"] 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads