How To Filter A Dictionary In TypeScript?

Do you want to filter a dictionary in Typescript? In the Typescript tutorial, I have explained how to filter a dictionary in Typescript.

Filter a dictionary in Typescript

Filtering a dictionary in TypeScript involves selecting specific key-value pairs that meet certain criteria from an existing dictionary. I will show you 3 different methods to filter a dictionary in Typescript.

  • Using Object.keys() and Array.prototype.filter()
  • Using Object.entries() and reduce()
  • Using Custom Filter Function

1. Using Object.keys() and Array.prototype.filter()

You can filter a dictionary in Typescript using Object.keys() and Array.prototype.filter(). Here is a complete code:

function filterDictionaryByKey(dict: { [key: string]: any }, predicate: (key: string) => boolean): { [key: string]: any } {
  let result: { [key: string]: any } = {};
  Object.keys(dict).filter(key => predicate(key)).forEach(filteredKey => {
    result[filteredKey] = dict[filteredKey];
  });
  return result;
}

// Example usage:
const myDictionary = {
  apple: 1,
  banana: 2,
  cherry: 3,
  date: 4
};

const filteredDictionary = filterDictionaryByKey(myDictionary, key => key.startsWith('a'));
console.log(filteredDictionary);

You can see the output below in the screenshot after running the code using Visual Studio Code.

filter dictionary in Typescript

2. Using Object.entries() and reduce()

Another method is using Object.entries() in combination with reduce() to filter the dictionary in Typescript.

function filterDictionaryByValue(dict: { [key: string]: number }, predicate: (value: number) => boolean): { [key: string]: number } {
  return Object.entries(dict).reduce((acc, [key, value]) => {
    if (predicate(value)) {
      acc[key] = value;
    }
    return acc;
  }, {} as { [key: string]: number });
}

// Example usage:
const filteredByValue = filterDictionaryByValue(myDictionary, value => value >= 3);
console.log(filteredByValue);

Once you run the code using Visual Studio Code, you can see the output below:

{ cherry: 3, date: 4 }
typescript filter dictionary

3. Using Custom Filter Function

You can also create a function that iterates over the dictionary entries to filter a dictionary in Typescript.

function customFilter<T>(dict: { [key: string]: T }, filterFn: (key: string, value: T) => boolean): { [key: string]: T } {
  let result: { [key: string]: T } = {};
  for (let [key, value] of Object.entries(dict)) {
    if (filterFn(key, value)) {
      result[key] = value;
    }
  }
  return result;
}

// Example usage:
const customFilteredDictionary = customFilter(myDictionary, (key, value) => value % 2 === 0);
console.log(customFilteredDictionary);

Conclusion

In this Typescript tutorial, I have explained how to filter a dictionary in Typescript.

  • Using Object.keys() and Array.prototype.filter()
  • Using Object.entries() and reduce()
  • Using Custom Filter Function

You may like the following tutorials:

Power Apps functions free pdf

30 Power Apps Functions

This free guide walks you through the 30 most-used Power Apps functions with real business examples, exact syntax, and results you can see.

Live Webinar

Quiz App Using SharePoint Framework (SPFx)

Learn to built a complete Quiz Management solution that enables admins to create and manage quizzes, categories, questions, and settings with an easy automated setup process in SharePoint. It also includes an interactive quiz experience for users and a powerful dashboard to track participation, analyze results, and view detailed performance reports with charts and answer insights.

📅 2nd June 2026 – 10:00 AM EST | 7:30 PM IST

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App