showing results for - "change array index position in javascript by up and down click"
Maya
17 Jan 2021
1// move up by 1 postion in Array
2const moveUp=(id)=> {
3  let index = arr.findIndex(e => e.id == id);
4  if (index > 0) {
5    let el = arr[index];
6    arr[index] = arr[index - 1];
7    arr[index - 1] = el;
8  }
9}
10
11// move dwon by 1 postion in Array
12const moveDown=(id)=> {
13  let index = arr.findIndex(e => e.id == id);
14  if (index !== -1 && index < arr.length - 1) {
15    let el = arr[index];
16    arr[index] = arr[index + 1];
17    arr[index + 1] = el;
18  }
19}