I'm having a hard time figuring out how to move an element of an array. For example, given the following:
var array = [ 'a', 'b', 'c', 'd', 'e'];
How can I write a function to move the element 'd'
to the left of 'b'
?
Or 'a'
to the right of 'c'
?
After moving the elements, the indexes of the rest of the elements should be updated. The resulting array would be:
array = ['a', 'd', 'b', 'c', 'e']
This seems like it should be pretty simple, but I can't wrap my head around it.
Create a temp variable and assign the value of the original position to it. Now, assign the value in the new position to original position. Finally, assign the value in the temp to the new position.
To move element to first position in array with JavaScript, we can use the spread operator and the array slice method. We call data. slice to get the last element and the first 3 elements respectively. Then we spread each array into a new array to populate the values of the returned arrays in the new array.
The array can be left rotated by shifting its elements to a position prior to them which can be accomplished by looping through the array and perform the operation arr[j] = arr[j+1]. The first element of the array will be added to the last of rotated array.
If you'd like a version on npm, array-move is the closest to this answer, although it's not the same implementation. See its usage section for more details. The previous version of this answer (that modified Array.prototype.move) can be found on npm at array.prototype.move.
I had fairly good success with this function:
function array_move(arr, old_index, new_index) { if (new_index >= arr.length) { var k = new_index - arr.length + 1; while (k--) { arr.push(undefined); } } arr.splice(new_index, 0, arr.splice(old_index, 1)[0]); return arr; // for testing }; // returns [2, 1, 3] console.log(array_move([1, 2, 3], 0, 1));
Note that the last return
is simply for testing purposes: splice
performs operations on the array in-place, so a return is not necessary. By extension, this move
is an in-place operation. If you want to avoid that and return a copy, use slice
.
Stepping through the code:
new_index
is greater than the length of the array, we want (I presume) to pad the array properly with new undefined
s. This little snippet handles this by pushing undefined
on the array until we have the proper length.arr.splice(old_index, 1)[0]
, we splice out the old element. splice
returns the element that was spliced out, but it's in an array. In our above example, this was [1]
. So we take the first index of that array to get the raw 1
there.splice
to insert this element in the new_index's place. Since we padded the array above if new_index > arr.length
, it will probably appear in the right place, unless they've done something strange like pass in a negative number. A fancier version to account for negative indices:
function array_move(arr, old_index, new_index) { while (old_index < 0) { old_index += arr.length; } while (new_index < 0) { new_index += arr.length; } if (new_index >= arr.length) { var k = new_index - arr.length + 1; while (k--) { arr.push(undefined); } } arr.splice(new_index, 0, arr.splice(old_index, 1)[0]); return arr; // for testing purposes }; // returns [1, 3, 2] console.log(array_move([1, 2, 3], -1, -2));
Which should account for things like array_move([1, 2, 3], -1, -2)
properly (move the last element to the second to last place). Result for that should be [1, 3, 2]
.
Either way, in your original question, you would do array_move(arr, 0, 2)
for a
after c
. For d
before b
, you would do array_move(arr, 3, 1)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With