循环/轮流访问数组

const times = 30 // 访问次数
const userArr = [1,2,3,4,5,6,7,8,9,10] // 需要循环访问的数据
for (let index = 0; index < times; index++) {
    let arr = [userArr[0],userArr[1]]
    userArr.push(userArr.shift())
    userArr.push(userArr.shift())
    console.log(arr);
}

// 输出结果

[ 1, 2 ]
[ 3, 4 ]
[ 5, 6 ]
[ 7, 8 ]
[ 9, 10 ]
[ 1, 2 ]
[ 3, 4 ]
[ 5, 6 ]
[ 7, 8 ]
[ 9, 10 ]
[ 1, 2 ]
[ 3, 4 ]
[ 5, 6 ]
[ 7, 8 ]
[ 9, 10 ]
[ 1, 2 ]
[ 3, 4 ]
[ 5, 6 ]
[ 7, 8 ]
[ 9, 10 ]
[ 1, 2 ]
[ 3, 4 ]
[ 5, 6 ]
[ 7, 8 ]
[ 9, 10 ]
[ 1, 2 ]
[ 3, 4 ]
[ 5, 6 ]
[ 7, 8 ]
[ 9, 10 ]



 

posted @ 2021-05-26 23:00  珂珂keo  阅读(104)  评论(0编辑  收藏  举报