How do you easily create empty matrices javascript?

前端 未结 17 1410
[愿得一人]
[愿得一人] 2020-12-04 16:29

In python, you can do this:

[([None] * 9) for x in range(9)]

and you\'ll get this:

[[None, None, None, None, None, None, No         


        
17条回答
  •  無奈伤痛
    2020-12-04 17:07

    better. that exactly will work.

    let mx = Matrix(9, 9);
    
    function Matrix(w, h){
        let mx = Array(w);
        for(let i of mx.keys())
            mx[i] = Array(h);
        return mx;
    }
    


    what was shown

    Array(9).fill(Array(9)); // Not correctly working
    

    It does not work, because all cells are fill with one array

提交回复
热议问题