問題:有兩個數組 分別是:javascript
1.["a","b","c"]java
2.["1","2","3"]數組
上面兩個數組合並函數
造成3.["a","1","b","2","c","3"]ui
上代碼spa
var arr1 = ["a", "b", "c"];
var arr2 = ["1", "2", "3"];
var arr3 = [],
len = arr1.length;
for (i = 0; i < len; i++) {
// arr3.splice(len.length - 1, 0, arr1[i], arr2[i]);
arr3.push(arr1[i]);
arr3.push(arr2[i]);
}
console.log(arr3)
複製代碼
若是要給初識兩個數組添加數據能夠將上面代碼轉變爲一個函數code
function newArr(new1, new2) {
var arr1 = ["a", "b", "c"];
arr1.push(new1);
var arr2 = ["1", "2", "3"];
arr2.push(new2);
var arr3 = [], len = arr1.length;
for (i = 0; i < len; i++) {
// arr3.splice(len.length - 1, 0, arr1[i], arr2[i]);
arr3.push(arr1[i]);
arr3.push(arr2[i]);
}
return arr3;
}
var ne = newArr("aAne", "cTwo");
console.log(ne);
複製代碼