的情況下,兩個數組有相同的長度,那麼你可以放心地使用這個
var arr1 = [[1,2,3], [4,5,6], [7,8,9]];
var arr2 = [["a","b","c"], ["d","e","f"], ["g","h","i"], ["j", "k","l"]];
var narr = [];
for(var ii = 0, len = Math.min(arr1.length, arr2.length); ii < len; ii++) {
narr[ii] = arr1[ii].concat(arr2[ii]);
}
,長度等於最小長度,這將創建一個新的數組兩個陣列arr1 & arr2
的結果將是:
[1,2,3,"a","b","c"]
[4,5,6,"d","e","f"]
[7,8,9,"g","h","i"]
,如果你不知道它們具有相同的長度,你可以簡單地改變這段代碼
var arr1 = [[1,2,3], [4,5,6], [7,8,9]];
var arr2 = [["a","b","c"], ["d","e","f"], ["g","h","i"], ["j", "k","l"]];
var narr = [];
for(var ii = 0, len = Math.max(arr1.length, arr2.length); ii < len; ii++) {
narr[ii] = arr1[ii] ? arr1[ii].concat(arr2[ii]) : arr2[ii];
}
結果將是
[1,2,3,"a","b","c"]
[4,5,6,"d","e","f"]
[7,8,9,"g","h","i"]
["j","k","l"]
謝謝很多,它工作得很好,我自己嘗試了forEach函數,但似乎我無法拿出一個工作代碼。非常感謝! – d4rk5h4rk