Alfred-Skyblue / vue-draggable-plus

Universal Drag-and-Drop Component Supporting both Vue 3 and Vue 2
https://vue-draggable-plus.pages.dev/en/
MIT License
3.05k stars 128 forks source link

Refactor `moveArrayElement` function #71

Closed chouchouji closed 9 months ago

chouchouji commented 9 months ago

This function aims to move an element in an array from one position to another.

I have another idea to realize it.

image

Alfred-Skyblue commented 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]   ❌