How to move an array element from one position to another?

Introduction

In this article, we will discover how to move an array element from one position to another? Arrays are the primary data structure in javascript which are usually used to store data. We can move the array element from one place to another by using the methods given below:

  • Temporary Variable
  • Array.splice( )

1) Temporary Variable:

The temporary variable method is the easiest method to move an array element from one position to another.

how to move an array element from one position to another

Let’s see the code example below which shows how to move an array element from one position to another? In which the element at index 3 interchanges index 6:

const arr = [1, 2, 3, 4, 5, 8, 9]
const temp = arr[3];
arr[3] = arr[6];
arr[6] = temp;
console.log(arr); // Output: [1, 2, 3, 9, 5, 8, 4]
Code Explanation

In the first step, a constant variable “arr” is declared which contains numbers in an array. In the next step, a temporary variable “temp” is declared that contains the index number of the value you want to change. In the third step, the array index 3 position is assigned to array index 6. In the second last step, the value of a temporary variable is assigned to the new position(index 6). In the last step, the final array is displayed in the console.

2) Array.splice():

The second best method is an array.splice() which can be used to move an array element from one position to another or to remove array elements and insert new elements in their index. Let’s see the code example below:

let arr = [1, 2, 3, 4, 5];
const elementToMove = arr[2]; // Element at position 2 (value: 3)
const currentPosition = 2;
const newPosition = 4;
Code Explanation:

The above code example shows that an array “arr” is declared with the values [1, 2, 3, 4, 5]. In the second step, the array index 2 is assigned to the constant variable elementToMove. In the next step, the element we want to move 2 is assigned to a constant variable currentPosition. After that another constant variable newPosition is declared which indicates the index where we want to move the element.

arr.splice(currentPosition, 1); // Remove element at current position
arr.splice(newPosition, 0, elementToMove); // Insert element at new position

console.log(arr); // Output: [1, 2, 4, 5, 3]
Code Explanation:

Now the splice method is applied to the arr array. In parenthesis, the currentPosition indicates the starting index and 1 indicates the number of elements to remove. In the next step, the splice is used again in which newPosition is the starting index, 0 indicates the number of elements to remove & the value of elementsToMove is added to the new position in the array. In the last step, the arr is displayed to the console.

Conclusion:

Hence we can move an array element from one position to another by using both the temporary variable method & array.splice method. The temporary variable method is simple, beginner-friendly, and easy to implement whereas the splice method looks complex. You can use any method according to your preference and requirements.

Similar Posts