JS, difference in array matrix and forEach behavior

后端 未结 3 1627
太阳男子
太阳男子 2021-01-19 19:53

I was doing some training tasks for my JS course and I got one where you must implement a function that takes a positive integer (n) and returns a matrix like the one below

3条回答
  •  渐次进展
    2021-01-19 20:31

    // your example is roughly equivalent to this.
    const innerArray = new Array(n).fill(0);
    const mat = new Array(n).fill(innerArray);
    
    (mat[0] === mat[1] === innerArray) === true;
    

    there is only 1 nested array, not n times array.

提交回复
热议问题