JavaScript中不规则矩阵的转置
我得到了这个矩阵,根据我从服务器收到的结果,宽度可以改变。基本上,它可以是JavaScript中不规则矩阵的转置,javascript,arrays,matrix,Javascript,Arrays,Matrix,我得到了这个矩阵,根据我从服务器收到的结果,宽度可以改变。基本上,它可以是 [undefined, undefined] [1, 2, 3, 4] [1, 2] [undefined, undefined, undefined] [1, 2, 3, 4, 5, 6] 我希望它是这样的 [undefined, 1, 1, undefined, 1] [undefined, 2, 2, undefined, 2] [undefined, 3,undefined, undefined, 3] [un
[undefined, undefined]
[1, 2, 3, 4]
[1, 2]
[undefined, undefined, undefined]
[1, 2, 3, 4, 5, 6]
我希望它是这样的
[undefined, 1, 1, undefined, 1]
[undefined, 2, 2, undefined, 2]
[undefined, 3,undefined, undefined, 3]
[undefined, 4, undefined, undefined, 4]
[undefined, undefined, undefined, undefined, 5]
[undefined, undefined, undefined, undefined, 6]
请注意,高度已更改为第一个示例的最大宽度。
我在谷歌上搜索了一下,我得到的最好的解决方案就是这个
array.map((row, i) => array.map(col => col[i]))
但是这个解决方案不会改变我的矩阵高度。我尝试了2个循环,但我认为我没有正确编码,因为我没有得到预期的结果。
如果有人能给我一点帮助,那就太棒了这里有几个步骤
const输入=[
[未定义,未定义],
[1, 2, 3, 4],
[1, 2],
[未定义,未定义,未定义],
[1, 2, 3, 4, 5, 6]
];
//首先查找最大长度
const maxw=Math.max(…input.map(m=>m.length));
//现在正常化
const norm_inputs=input.map(m=>
(新数组(maxw)).concat(m).slice(-maxw)
);
//现在轮换。。
常量输出=(新数组(maxw).fill(0)).map(
(row,i)=>norm_inputs.map(col=>col[i])
这里涉及几个步骤
const输入=[
[未定义,未定义],
[1, 2, 3, 4],
[1, 2],
[未定义,未定义,未定义],
[1, 2, 3, 4, 5, 6]
];
//首先查找最大长度
const maxw=Math.max(…input.map(m=>m.length));
//现在正常化
const norm_inputs=input.map(m=>
(新数组(maxw)).concat(m).slice(-maxw)
);
//现在轮换。。
常量输出=(新数组(maxw).fill(0)).map(
(row,i)=>norm_inputs.map(col=>col[i])
有很多方法可以解决这个问题(如其他答案所示)。我将这样做,因为这样可以最大限度地减少我们对数据的迭代次数
const数据=[
[未定义,未定义],
[1, 2, 3, 4],
[1, 2],
[未定义,未定义,未定义],
[1, 2, 3, 4, 5, 6],
];
//获取最长数组的长度
const maxLen=data.reduce((max,{length})=>Math.max(max,length),0);
//创建一组新的数组
const result=Array.from({length:maxLen},(u,i)=>data.map(col=>col[i]);
控制台日志(结果)代码>有很多方法可以解决这个问题(如其他答案所示)。我将这样做,因为这样可以最大限度地减少我们对数据的迭代次数
const数据=[
[未定义,未定义],
[1, 2, 3, 4],
[1, 2],
[未定义,未定义,未定义],
[1, 2, 3, 4, 5, 6],
];
//获取最长数组的长度
const maxLen=data.reduce((max,{length})=>Math.max(max,length),0);
//创建一组新的数组
const result=Array.from({length:maxLen},(u,i)=>data.map(col=>col[i]);
控制台日志(结果)代码>假设您的矩阵存储在矩阵中
const transformed = [];
const maxWidth = matrix.reduce((max, current) => {
return (current.length > max) ? current.length : max;
}, 0);
// Loop over each column
for (let i = 0; i < maxWidth; i++) {
const tRow = [];
// Loop over each row of the current column
for (const row of matrix) {
tRow.push(row[i]);
}
transformed.push(tRow);
}
const transformed=[];
常量maxWidth=矩阵。减少((最大,当前)=>{
返回值(当前长度>最大值)?当前长度:最大值;
}, 0);
//在每列上循环
for(设i=0;i
假设您的矩阵存储在矩阵中
const transformed = [];
const maxWidth = matrix.reduce((max, current) => {
return (current.length > max) ? current.length : max;
}, 0);
// Loop over each column
for (let i = 0; i < maxWidth; i++) {
const tRow = [];
// Loop over each row of the current column
for (const row of matrix) {
tRow.push(row[i]);
}
transformed.push(tRow);
}
const transformed=[];
常量maxWidth=矩阵。减少((最大,当前)=>{
返回值(当前长度>最大值)?当前长度:最大值;
}, 0);
//在每列上循环
for(设i=0;i
您的解决方案几乎成功,但即使矩阵具有预期的高度,它仍然只得到输入矩阵高度的元素数。@Tiagonf我为切片设置了-6,而不是-maxw,请立即尝试。您的解决方案几乎成功,但即使矩阵具有预期的高度,它仍然只得到了输入矩阵高度的元素数。@Tiagonf我为切片设置了-6而不是-maxw,现在试试。我认为如果调用map迭代元素行而不是coldata.map(row=>row[I])
我认为如果调用map迭代元素行而不是coldata.map(row=>row[I]),会更清晰一些。