Front-end

不定义任何变量交互数组中相邻元素

不定义任何变量交互数组中相邻元素

不定义任何变量交互数组中相邻元素

要点

  • 使用slice方法将需要交换的元素作为数组取出
  • 使用reverse方法将元素交换位置
  • 使用...操作符将交换后的数组展开
  • 将展开结果作为splice方法的后两个参数,从而取代原来的两个元素

代码

/**
 * [exchangeAdjacentElements 交互数组中相邻元素]
 * @param  {[type]} arr [目标数组]
 * @param  {[type]} i   [需要交换的两个相邻元素中位置靠前的元素下标]
 */
const exchangeAdjacentElements = (arr, i) => arr.splice(i, 2, ...arr.slice(i, i + 2).reverse())

const arr = [1, 2, 3, 4, 5]

exchangeAdjacentElements(arr, 2)

console.log(arr)
// Expect: [1, 2, 4, 3, 5]
js

提示

由于使用了splice方法,所以该方法会改变原数组!

评论