js交错合并数组

let arr1 = [1,2,3,4,5,6]
let arr2 = ['1a','2a','3b',1]

let arr3 = [];
for(let i in arr1.length > arr2.length ? arr1 : arr2){
	if (arr1[i]) arr3[2*i] = arr1[i]
	if (arr2[i]) arr3[2*i+1] = arr2[i]
}
let arr4 = arr3.filter(e=>e)

console.log(arr4); // [1, "1a", 2, "2a", 3, "3b", 4, 1, 5, 6]
posted @ 2021-02-22 15:03  余以为  阅读(147)  评论(0编辑  收藏  举报