// 合并两个数组
var a = [1, 2, 4, 8,10];
var b = [3, 6, 7, 8, 11];
function merge(arr1, arr2) {
const resultArr = [];
while(arr1.length >0 && arr2.length>0) {
arr1[0] < arr2[0] ? resultArr.push(arr1.splice(0, 1)[0]) : resultArr.push(arr2.splice(0, 1)[0]);
}
return resultArr.concat(arr1, arr2);
}
console.log(merge(a, b)); // 输出[1, 2, 3, 4, 6, 7, 8, 8, 10, 11]
// 字符串
网友评论