const fillSquareMatrix = (size) => { return Array(size) .fill() .map((u,y) => Array(size) .fill() .map((u,x) => y * size + x + 1)); }; console.log(fillSquareMatrix(3));