javascript - 如何将数组更改为(mxn)数组
问题描述
例如,如何将数组(向量)更改[1, 2,...,16]
为[[1,2,3,4],..., [13, 14, 15, 16]]
. 我写
function IntoMatrix(m,n, array){
temp = [];
Temp = [];
for (j=0; j<array.length; j+=m){
for (i=0; i<n; i++){
temp.push(array[i+j]);
}
Temp.push(temp);
}
return Temp;
}
但肯定有问题。您认为我应该使用inconcat
代替push
方法Temp
吗?
解决方案
对于每个内部 for 循环,您需要重置temp=[]
.
function IntoMatrix(m,n, array){
temp = [];
Temp = [];
for (j=0; j<array.length; j+=m){
temp = [];
for (i=0; i<n; i++){
temp.push(array[i+j]);
}
Temp.push(temp);
}
return Temp;
}
console.log(IntoMatrix(4,4,[1,2,3,4,5,6,7,8]));
推荐阅读
- go - 尝试在测试模式下在 VSCode 调试器上调试 golang 时出现奇怪的错误
- python - 我如何向不和谐请求添加附件?
- json - 在 delphi-xe 中使用 NetHTTPClient1 发布带有身份验证的 json
- python - 如何修复“CSRF 验证失败。请求中止。” 请?
- flutter - 错误:无法处理本地通知上的方法调用
- docker - 向 Traefik 添加自定义标头会产生奇怪的行为
- javascript - 对象构造函数,将信息从表单传递到 HTML
- python - 如何洗牌html字段?- django
- css - 图像周围的阴影效果而不是图像边框
- xml - Delphi 使用 XML 格式的 Rest Client 示例