Closed chouchouji closed 9 months ago
Firstly, thank you for your suggestions. However, the two functions you described above essentially serve different purposes. Currently, I am using a function to move an element to a target position, whereas the functionality you implemented involves swapping the positions of two elements.
const arr1 = [1, 2, 3, 4, 5]
arr1.splice(0, 0, arr1.splice(4, 1)[0])
console.log(arr1) // [5, 1, 2, 3, 4] ✅
// ===========================
const arr2 = [1, 2, 3, 4, 5]
;[arr2[0],arr2[4]] = [arr2[4],arr2[0]]
console.log(arr2) // [5, 2, 3, 4, 1] ❌
This function aims to move an element in an array from one position to another.
I have another idea to realize it.