This typescript tutorial, we will discuss the typescript dictionary with examples. We will see how we can create a dictionary in typescript? how can we get the length of the dictionary in typescript?
If you are using new to typescript read my previous article on TypeScript Tutorial (step by step).
Download TypeScript (Latest Version) (TypeScript 3.4).
TypeScript Dictionary
Dictionaries are commonly used collections. They provide the benefit of quickly looking up values based on a supplied Key and these lookups are extremely fast as they don’t rely on iterating the collection to locate them.
Create Dictionary in typescript
Lodash has a simple Dictionary implementation and has good TypeScript support :
Command: install npm install lodash @types/lodash –save
Import and usage:
import { Dictionary } from “lodash”;
Example:
import { Dictionary } from "lodash";
var country:Dictionary<string>= {};
country[1] = 'India';
country[2] = 'Aus';
country[3] = 'Eng';
console.log(country)
console.log(country[1])
console.log(country[3])
//output:
{ '1': 'India', '2': 'Aus', '3': 'Eng' }
India
Eng
Get length of the dictionary in typescript
Use the following syntax to get the length of the dictionary in typescript.
Syntax: Object.keys(dictionaryName).length)
Example:
var country:Dictionary<string>= {};
country[1] = 'India';
country[2] = 'Aus';
country[3] = 'Eng';
console.log(Object.keys(country).length);//3
Iterate through TypeScript Dictionary
There are different ways to iterate through the values from typescript dictionary.
for..of on dictionary in typescript
Example: Flip through the enumerable keys defined on an object.
import { Dictionary } from "lodash";
var country:Dictionary<string>= {};
country[1] = 'India';
country[2] = 'Aus';
country[3] = 'Eng';
console.log(country)
for (const key of Object.keys(country)) {
const value = country[key]
console.log(`${key} -> ${value}`)
}
//output:
{ '1': 'India', '2': 'Aus', '3': 'Eng' }
1 -> India
2 -> Aus
3 -> Eng
for..in on dictionary in typescript
Example: Flips through the keys defined on the object and its prototype chain.
var country:Dictionary<string>= {};
country[1] = 'India';
country[2] = 'Aus';
country[3] = 'Eng';
console.log(country)
for (const key in country) {
const value = country[key]
console.log(`${key} -> ${value}`)
}
//output:
{ '1': 'India', '2': 'Aus', '3': 'Eng' }
1 -> India
2 -> Aus
3 -> Eng
Interface in dictionary in TypeScript
Example: implement dictionary using interface.
interface IPerson {
firstName: string;
lastName?: string;
}
var persons:{ [id: string] : IPerson; } = {};
persons["p101"] = { firstName: "lakshmi", lastName: "k" };
persons["p102"] = { firstName: "preeti" };
console.log(persons);
//output:
{ p101: { firstName: 'lakshmi', lastName: 'k' },
p102: { firstName: 'preeti' } }
You may like following TypeScript Tutorials:
- TypeScript Enum (With Examples)
- TypeScript Date (With Examples)
- TypeScript Map (Detailed Tutorial with Examples)
- TypeScript Set and Weakset (with Examples)
Hope this typescript tutorial helps to learn TypeScript Dictionary with examples.
I am Bijay a Microsoft MVP (8 times – My MVP Profile) in SharePoint and have more than 15 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com