Можно так
function create3dMatrix(size) {
return Array.from(Array(size), () => {
return Array.from(Array(size), () => {
return Array(size).fill(0);
});
});
}
console.log(JSON.stringify(create3dMatrix(2)));
Со стрелочными:
const create3dMatrix = size =>
Array.from(Array(size), () =>
Array.from(Array(size), () =>
Array(size).fill(0)
)
)