'How do you do Numpy.eye in JavaScript?

How to create the np.eye function in JavaScript? Or what would be the numpy.eye equivalent in JavaScript?

I would like a function that creates the "Identity matrix" in 2d dimensions, and you can change the number of rows, columns and the index of the diagonal. https://numpy.org/devdocs/reference/generated/numpy.eye.html

This doesn't take care of M,N,k @Andy

function eye(n){
var t=[];
for(var i=0;i<n;i++){
var p=[]
for(var j=0;j<n;j++){
p.push(j==i?1:0)
}
t.push(p)
}}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source