翻转数组

const arrayReverseChangeMetaArray = (arr = [1,2,3]) => {
    const res = []
    while(arr.length){
        res.push(arr.pop())
    }
    return res
}

const arrayReverse = (arr = [1,2,3]) => {
    let i = 0, j = arr.length - 1
    while(i < j){
        [arr[i], arr[j]] = [arr[j], arr[i]]
        i++
        j--
    }
    return arr
}

Array.prototype.myReserve = function () {
    let startIdx = 0,
        endIdx = this.length - 1
    while (startIdx < endIdx) {
        [this[startIdx], this[endIdx]] = [this[endIdx], this[startIdx]]
        startIdx++
        endIdx--
    }
    return this
}

  

posted @ 2023-02-03 14:21  671_MrSix  阅读(4)  评论(0编辑  收藏  举报