我有三個陣列
arr1 = ["a1", "a2", "a3"]
arr2 = ["b1", "b2", "b3"]
arr3 = ["c1", "c2", "c3"]
我希望三個新陣列看起來像這樣
arr4 = ["a1", "b1", "c1"]
arr5 = ["a2", "b2", "c2"]
arr6 = ["a3", "b3", "c3"]
如何在 JavaScript 中完成獲取這些新陣列?
uj5u.com熱心網友回復:
這是一種方法 - 將源陣列放在一個變數中,使用最短源陣列的長度作為迭代器長度并動態創建新變數。
let arr1 = ["a1", "a2", "a3"]
let arr2 = ["b1", "b2", "b3"]
let arr3 = ["c1", "c2", "c3"]
// just in case they're not all the same length, we'll use the shortest one as the iterator length
let ct = 0,
st = 4,
arrs = [arr1, arr2, arr3],
len = Math.min(arr1.length, arr2.length, arr3.length);
while (ct < len) {
this[`arr${st ct}`] = arrs.map(a => a[ct]);
ct ;
}
console.log(arr5)
uj5u.com熱心網友回復:
let mixedArr = [arr1, arr2, arr3];
let res = [];
for (i=0;i<mixedArr.length;i ) {
res.push(mixedArr.map(arr => arr[i]))
}
console.log(res)
然后你可以從完成的陣列中獲取你需要的東西
uj5u.com熱心網友回復:
您可以使用兩個回圈來執行此操作,但您將在一個陣列中添加所有陣列以執行回圈,我們在另一個陣列中提取新陣列
這是你的代碼
let arr1 = ["a1", "a2", "a3"],
arr2 = ["b1", "b2", "b3"],
arr3 = ["c1", "c2", "c3"];
我們將所有陣列添加到一個變數中
let allArr = [arr1, arr2, arr3];
我們制作一個陣列來提供所有新陣列
let resultArr = [];
我們使第一個回圈在每個陣列中回圈
for (let i = 0; i < allArr.length; i ) {
我們創建一個名為 res 的陣列來創建新陣列
let res = [];
我們在allArr變數上創建另一個回圈,但是為了改變我們使用一個foreach回圈
allArr.forEach((arr) => {
res.push(arr[i]);
});
然后我們將 res 添加到resultArrVairable 之后
resultArr.push(res);
我們關閉了回圈
}
這是你的代碼
let arr1 = ["a1", "a2", "a3"],
arr2 = ["b1", "b2", "b3"],
arr3 = ["c1", "c2", "c3"];
let allArr = [arr1, arr2, arr3];
let resultArr = [];
for (let i = 0; i < allArr.length; i ) {
let res = [];
allArr.forEach((arr) => {
res.push(arr[i]);
});
resultArr.push(res);
}
我添加此回圈以僅在控制臺中記錄陣列。現場觀看
resultArr.forEach((arr) => {
console.log(arr);
});
let arr1 = ["a1", "a2", "a3"],
arr2 = ["b1", "b2", "b3"],
arr3 = ["c1", "c2", "c3"];
let allArr = [arr1, arr2, arr3];
let resultArr = [];
for (let i = 0; i < allArr.length; i ) {
let res = [];
allArr.forEach((arr) => {
res.push(arr[i]);
});
resultArr.push(res);
}
resultArr.forEach((arr) => {
console.log(arr);
});
我希望我的解釋很好:)
uj5u.com熱心網友回復:
這可能是實作預期目標的一種可能解決方案。
代碼片段
// method to transpose the arrays
const transpose = arr => {
// first create a placeholder "result" 2D array
const result = [
...Array(Math.min(
...arr.map(a => a.length)
))
].map((_, i) => [
...Array(arr[i].length)
].map(x => ""));
// now, traverse through input 2D array &
// populate "result" by transposing row & col
arr.forEach((row, rIdx) =>
row.forEach((elt, cIdx) =>
result[cIdx][rIdx] = elt
)
)
return result;
};
const arrayOfArrays = [
["a1", "a2", "a3"],
["b1", "b2", "b3"],
["c1", "c2", "c3"]
];
console.log(
'test case 1: ',
JSON.stringify(arrayOfArrays),
'\nresult: ',
JSON.stringify(transpose(arrayOfArrays))
);
// using with the context of the question
const arr1 = ["a1", "a2", "a3"];
const arr2 = ["b1", "b2", "b3"];
const arr3 = ["c1", "c2", "c3"];
console.log(
'using arr1, 2, 3: \n',
JSON.stringify({arr1, arr2, arr3}),
'\nraw-result: ',
JSON.stringify(transpose([arr1, arr2, arr3]))
);
// getting the result arrays separately
const [arr4, arr5, arr6] = transpose([arr1, arr2, arr3]);
console.log(
'showing result arrays arr4, 5, 6: \n',
JSON.stringify({arr4, arr5, arr6})
);
// Testing with "20" element arrays
const alph = "abcdefghijklmnopqrstuvwxyz".split('');
const num = 20;
const twentyArr = [...Array(num).keys()].map(k => [...Array(num).keys()].map((_, i) => `${alph[k]}${i 1}`));
console.log(
'Testing for twenty elements array: \n',
JSON.stringify(twentyArr),
'\nresult:\n',
JSON.stringify(transpose(twentyArr))
);
.as-console-wrapper { max-height: 100% !important; top: 0 }
解釋
上面代碼段中提供的行內注釋。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/460471.html
標籤:javascript 数组
