TypeScript Dictionary (With Examples)

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:

Hope this typescript tutorial helps to learn TypeScript Dictionary with examples.

>