Typescript - multidimensional array initialization

后端 未结 5 1958
刺人心
刺人心 2020-12-08 05:55

I\'m playing with Typescript and I wonder, how to properly instantiate and declare multidimensional array. Here\'s my code:

class Something {
    private thi         


        
5条回答
  •  广开言路
    2020-12-08 06:43

    You can do the following (which I find trivial, but its actually correct). For anyone trying to find how to initialize a two-dimensional array in TypeScript (like myself).

    Let's assume that you want to initialize a two-dimensional array, of any type. You can do the following

    const myArray: any[][] = [];

    And later, when you want to populate it, you can do the following:

    myArray.push([]);

    A short example of the above can be the following:

    const myArray: string[][] = [];
    myArray.push(["value1", "value2"]);

提交回复
热议问题