JS将俩个有序数组合并为一个有序数组

function arrSort(arr1, arr2) { 
    var [i,j] = [0,0];
    let newArr = [];
    while(i < arr1.length || j <arr2.length) { 
        if (arr1[i] < arr2[j]) { 
            newArr.push(arr1[i]);
            i++
        } else if (arr1[i] > arr2[j]) { 
            newArr.push(arr2[j])
            j++
        } else { 
            if(arr1[i]) newArr.push(arr1[i]);
            if(arr2[j]) newArr.push(arr2[j]);
            i++;
            j++
        }
    }
    return newArr
}

let arrs = arrSort([2,4,6,7], [1,2,3,5,8,9,10,33,44,49]);

console.log(arrs);
    原文作者:高同学呀
    原文地址: https://blog.csdn.net/weixin_40629549/article/details/104658913
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞