[Javascript] Swap array item in place using splice()

function swap(array, i, j) {
  const [item] = array.splice(i, 1) // get item and remove this item from array
  array.splice(j, 0, item)
}

 

posted @ 2022-07-10 21:14  Zhentiw  阅读(22)  评论(0编辑  收藏  举报