[英]Slicing 2d array in Javascript
This is the Python code which I want to convert to Javascript!这是我想转换为 Javascript 的 Python 代码!
y[i] = x[i][:j] + x[i][j] + x[i][j + 1:]
I tried using the slice function but I cannot correctly implement it for a 2d array.我尝试使用切片 function 但我无法为二维数组正确实现它。
So that's just some python fancy work for list comprehension but I believe you could accomplish such a thing with:所以这只是一些用于列表理解的 python 奇特的工作,但我相信你可以通过以下方式完成这样的事情:
const x = [
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9]
];
var y = new Array(81);
for (i = 0; i < x.length; i++) {
var row = new Array(20);
for (j = 0; j < x.length; j++) {
row.push(x[i].slice(j).concat(x[i][j]).concat(x[i].slice(0, j+1)))
}
y.push(row);
}
for(k = 0; k < y.length; k++) {
console.log(y[k]);
}
I'm not exceptionally good with javascript, but let me know if that helps.我对 javascript 不是特别好,但如果有帮助,请告诉我。 -Or- provide some more input/output so I can test with, any additional code, what you've tried or what you're expected output may look like. - 或 - 提供更多输入/输出,以便我可以测试任何其他代码、您尝试过的或您期望的 output 可能看起来像什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.