请问js有什么好办法让让两个数组合并在一起,以奇偶数穿插的方式
例如:
var a = [“a”,"b","c"]
var b = ["1","2","3"]
//得到:var c = ["a","1","b","2","c","3"]
//a,b数组长度不固定,动态获取情况下
请问js有什么好办法让让两个数组合并在一起,以奇偶数穿插的方式
例如:
var a = [“a”,"b","c"]
var b = ["1","2","3"]
//得到:var c = ["a","1","b","2","c","3"]
//a,b数组长度不固定,动态获取情况下
var a = ['a', 'b', 'c', 'd', 'e']
var b = ['1', '2', '3', '4', '5', '6']
function func(arr1, arr2, result = []) {
if (arr1.length >= arr2.length) {
for (let i = 0; i < arr1.length; i++) {
if (arr1[i]) {
result.push(arr1[i])
}
if (arr2[i]) {
result.push(arr2[i])
}
}
} else {
for (let i = 0; i < arr2.length; i++) {
if (arr1[i]) {
result.push(arr1[i])
}
if (arr2[i]) {
result.push(arr2[i])
}
}
}
return result
}
const res = func(a, b)
console.log('res', res)