Array.moveWithin

Similar to array.copyWithin but instead of copying, it will move the element to the new location without an intermediate stage that changes the length thus avoiding having to use two splice operations to achieve the same goal.

1 Like

copyWithin doesn't change the length. Maybe if you could post an example of how the two would be different?

It sounds like the idea of moveWithin would be to insert and shift rather than overwrite

a = [1,2,3]
a.copyWithin(0, 2, 3)
a // [3,2,3]

// vs

a = [1,2,3]
a.moveWithin(0, 2, 3)
a // [3,1,2]

// matching

a = [1,2,3]
a.splice(0, 0, ...a.splice(2, 1)) // changes length
a // [3,1,2]

I'm not personally interested in ever adding more mutator methods to Array.prototype.

What are the use cases that moveWithin would solve?

1 Like

Thanks, I follow now :slightly_smiling_face: