Typescript - multidimensional array initialization

后端 未结 5 1951
刺人心
刺人心 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:33

    Here is an example of initializing a boolean[][]:

    const n = 8; // or some dynamic value
    const palindrome: boolean[][] = new Array(n)
                                       .fill(false)
                                       .map(() => new Array(n)
                                       .fill(false));
    
    0 讨论(0)
  • 2020-12-08 06:34

    If you want to do it typed:

    class Something {
    
      areas: Area[][];
    
      constructor() {
        this.areas = new Array<Array<Area>>();
        for (let y = 0; y <= 100; y++) {
          let row:Area[]  = new Array<Area>();      
          for (let x = 0; x <=100; x++){
            row.push(new Area(x, y));
          }
          this.areas.push(row);
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-08 06:39

    You only need [] to instantiate an array - this is true regardless of its type. The fact that the array is of an array type is immaterial.

    The same thing applies at the first level in your loop. It is merely an array and [] is a new empty array - job done.

    As for the second level, if Thing is a class then new Thing() will be just fine. Otherwise, depending on the type, you may need a factory function or other expression to create one.

    class Something {
        private things: Thing[][];
    
        constructor() {
            this.things = [];
    
            for(var i: number = 0; i < 10; i++) {
                this.things[i] = [];
                for(var j: number = 0; j< 10; j++) {
                    this.things[i][j] = new Thing();
                }
            }
        }
    }
    
    0 讨论(0)
  • 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([<your value goes here>]);

    A short example of the above can be the following:

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

    0 讨论(0)
  • 2020-12-08 06:45

    Beware of the use of push method, if you don't use indexes, it won't work!

    var main2dArray: Things[][] = []
    
    main2dArray.push(someTmp1dArray)
    main2dArray.push(someOtherTmp1dArray)
    

    gives only a 1 line array!

    use

    main2dArray[0] = someTmp1dArray
    main2dArray[1] = someOtherTmp1dArray
    

    to get your 2d array working!!!

    Other beware! foreach doesn't seem to work with 2d arrays!

    0 讨论(0)
提交回复
热议问题