moving within array values code example

Example 1: how to move an element of an array in javascript

function moveElement(array,initialIndex,finalIndex) {
	array.splice(finalIndex,0,array.splice(initialIndex,1)[0])
	console.log(array);
	return array;
}
// Coded By Bilal

Example 2: change array index position in javascript by up and down click

// 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;
  }
}