📌  相关文章
📜  通过上下单击更改 javascript 中的数组索引位置 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:51.231000             🧑  作者: Mango

代码示例1
// move up by 1 postion in Array
const moveUp=(id)=> {
  let index = arr.findIndex(e => e.id == id);
  if (index > 0) {
    let el = arr[index];
    arr[index] = arr[index - 1];
    arr[index - 1] = el;
  }
}

// move dwon by 1 postion in Array
const moveDown=(id)=> {
  let index = arr.findIndex(e => e.id == id);
  if (index !== -1 && index < arr.length - 1) {
    let el = arr[index];
    arr[index] = arr[index + 1];
    arr[index + 1] = el;
  }
}