我想以特定方式合并某些数组。它现在看起来像什么:
[key1, key2, key3, key4]
[val1, val2, val3, val4]
[val1, val2, val3, val4]
[val1, val2, val3, val4]
[val1, val2, val3, val4]
我希望它看起来如何
[key1, val1, val1, val1, val1]
[key2, val2, val2, val2, val2]
[key3, val3, val3, val3, val3]
[key4, val4, val4, val4, val4]
希望你们有一个解决方案,谢谢你!
PS:如果有什么东西不够清楚只需评论。
答案 0 :(得分:0)
只需更改行和列。基本上这是:r[j][i] = x[i][j]
var data = [['key1', 'key2', 'key3', 'key4'], ['val11', 'val21', 'val31', 'val41'], ['val12', 'val22', 'val32', 'val42'], ['val13', 'val23', 'val33', 'val43'], ['val14', 'val24', 'val34', 'val44']],
pivot = function (data) {
var r = [];
data.forEach(function (a, i) {
a.forEach(function (b, j) {
r[j] = r[j] || [];
r[j][i] = b;
});
});
return r;
}(data);
document.write('<pre>' + JSON.stringify(pivot, 0, 4) + '</pre>');
&#13;
答案 1 :(得分:0)
您可以创建这些数组的数组并填充新数组,如下所示:
var array1 = ["key1", "key2", "key3", "key4"];
var array2 = ["val1", "val2", "val3", "val4"];
var array3 = ["val1", "val2", "val3", "val4"];
var array4 = ["val1", "val2", "val3", "val4"];
var array5 = ["val1", "val2", "val3", "val4"];
allArrays = [array1,array2,array3,array4,array5];
array11 = []
array22 = []
array33 = []
array44 = []
for(var i = 0; i < allArrays.length; i++){
array11.push(allArrays[i][0]);
array22.push(allArrays[i][1]);
array33.push(allArrays[i][2]);
array44.push(allArrays[i][3]);
}