How do you easily create empty matrices javascript?

前端 未结 17 1447
[愿得一人]
[愿得一人] 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 16:55

    Use this function or some like that. :)

    function createMatrix(line, col, defaultValue = 0){ 
        return new Array(line).fill(defaultValue).map((x)=>{ return new Array(col).fill(defaultValue); return x; }); 
    }
    var myMatrix = createMatrix(9,9);
    

提交回复
热议问题