How To Declare A Two-dimensional Array In Typescript

Declare a Two-dimensional Array in TypeScript

To declare a two-dimensional array in TypeScript you need to pay attention to double square bracket. It’s really important. So how to do it? Let’s go into detail now.

Declare a two-dimensional array in Typescript

Use the double square bracket

To declare a two-dimensional array in Typescript, you can use the double square bracket.

Syntax:

string[][]

Here the syntax indicates that it declares an array that contains the array type element with string type.

Example:

const anArray: string[][] = [["Hello"], ["From"], ["Learn"], ["Share"], ["IT"]];

for (let i = 0; i < anArray.length; i++) {
  console.log(anArray[i]);
}

Output:

[LOG]: [["Hello"], ["From"], ["Learn"], ["Share"], ["IT"]] 
[LOG]: ["Hello"] 
[LOG]: ["From"] 
[LOG]: ["Learn"] 
[LOG]: ["Share"] 
[LOG]: ["IT"]

You can also set the type for the second-level array as a custom type.

Example:

type infor = {
    name: string,
    age: number,
    country: string
};
  
const anArray: infor[][] = [
    [{ name: "Jone", age: 25, country: "India" }],
    [{ name: "Anton", age: 30, country: "American" }],
    [{ name: "Anna", age: 20, country: "Italy" }],
];
  
for (let i = 0; i < anArray.length; i++) {
    console.log(anArray[i]);
}

Output:

[LOG]: [{ "name": "Jone", "age": 25, "country": "India" }] 
[LOG]: [{ "name": "Anton", "age": 30, "country": "American" }] 
[LOG]: [{ "name": "Anna", "age": 20, "country": "Italy" }]

Here I set the type for the second level array as the infor object that has a name, age, and country property. 

Some common method

You can still use the method in both arrays like normal.

Example:

const anArray: string[][] = [
    ["Hello", "Hi"],
    ["From"],
    ["Learn"],
    ["Share"],
    ["IT"],
];
  
anArray[0].pop();
console.log(anArray);
  
const result = anArray.join(" ");
console.log(result);

Here I use the pop method in the first array element to remove one element. With the join method, I can concatenate all elements in an array.

If you want to add a new array element, you can use the push method.

Syntax:

Arr.push(value)

Parameters:

  • Value: value that you want to push into the array.

Example:

type infor = {
    name: string,
    age: number,
    country: string
};
  
const anArray: infor[][] = [
    [{ name: "Jone", age: 25, country: "India" }],
    [{ name: "Anton", age: 30, country: "American" }],
    [{ name: "Anna", age: 20, country: "Italy" }],
];
  
console.log(anArray);
anArray.push([{ name: "ManCha", age: 24, country: "Jamaica" }]);
console.log(anArray);

Output:

[LOG]: [[{
  "name": "Jone",
  "age": 25,
  "country": "India"
}], [{
  "name": "Anton",
  "age": 30,
  "country": "American"
}], [{
  "name": "Anna",
  "age": 20,
  "country": "Italy"
}], [{
  "name": "ManCha",
  "age": 24,
  "country": "Jamaica"
}]] 

The solution to do flat the two-dimensional array

If you want to convert a two-dimensional array to a one-dimensional array, you can use the flat method.

Syntax:

arr.flat()

With the flat method, you can remove one level in the array.

Example:

const anArray: number[][] = [[0], [1], [2], [3], [4], [5], [6]];
console.log(anArray.flat());

Output:

[LOG]: [0, 1, 2, 3, 4, 5, 6]

So here, with the flat method, I can convert a two-dimensional array to a normal array.

Summary

In this article, I showed you how to declare a two-dimensional array in Typescript. You can use type[][] syntax. I also you some ways to work with two-dimensional arrays and how to flat two-dimensional arrays. Good luck for you!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *