sam rodrigues
sam rodrigues

Reputation: 580

Declartion of array in TypeScript

i am new to type Script . wanted to know how can declare array[key ,value] ,and how do you declare something like this array[key,array[key,value]] in type Script.

Upvotes: 1

Views: 3260

Answers (2)

Fenton
Fenton

Reputation: 251262

You could achieve this using a simple interface to describe a key/value pair. In my example I have made it generic as this allows it to satisfy the simple and nested use cases.

interface KeyValuePair<T> {
    key: string;
    value: T;
}

//Simple

var arr: KeyValuePair<string>[] = [];

arr.push({key: 'A', value: 'Value For A'});
arr.push({key: 'B', value: 'Value For B'});

// Nested

var nested: KeyValuePair<KeyValuePair<number>[]>[] = [];

nested.push({
    key: 'A',
    value: [
        { key: 'AA', value: 1 },
        { key: 'AB', value: 2 }
    ]
});

Upvotes: 6

Jalpesh Vadgama
Jalpesh Vadgama

Reputation: 14266

You can do some thing like this.

 var fstarry: string[] = ['C','Sharp','Corner','Dot','Net','Heaven','Modeling','Corner']; 

Here is great article for that http://www.c-sharpcorner.com/UploadFile/5089e0/array-object-in-typescriptpart5/

Upvotes: 0

Related Questions