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

In JavaScript we can access an array element as other programming languages like C, C++, Java etc. Also there is a method called splice() in JavaScript, by which an array can be removed or replaced by another element for an index. So to move an array element from one array position to another we can splice() method or we can simply use array indexing ([]). 
 

Example : Simple code for moving an array element from one array position to another, without using any functions. 

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
var arr = ["C++", "Java", "JS", "Python"];
  
document.write("Original array: "+arr+"<br>");
  
    // Position where from the element is 
    // going to move here 'python' is moved 
    var x = 3; 
      
    // Position at which element is to be 
    // moved here 'python' is moved to  
    // index 1 which is index of 'Java'
    var pos = 1; 
  
    // Store the moved element in a temp 
    // variable
    var temp = arr[x]; 
      
    // shift elements forward 
    var i;
    for (i = x; i >= pos; i--) 
        {
            arr[i] = arr[i - 1]; 
        }
      
    // Insert moved element at position  
    arr[pos] = temp; 
  
document.write("<br>After move: "+arr+"<br>");
  
</script>

chevron_right


Output : 

Original array: C++,Java,JS,Python

After move: C++,Python,Java,JS

Example : Now move an array element from one array position to another using functions, 

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    var arr = ["C++ ", "Java ", "JS ",
               "Ruby ", "Python "];
  
    //     Print the array before moving
    document.write("Original array: "+arr+"<br>");
  
    // Position where from the element is 
    // going to move here 'Ruby' is moved 
    var moveEle = 3;
  
    // Position at which element is to be moved 
    // here 'Ruby' is moved to  index 1 which is 
    // index of 'Java'
    var moveToIndx = 1;
  
    // If acctual index of moved element is 
    // less than 0 when 'moveEle += array size'
    while (moveEle < 0)
    {
        moveEle += arr.length;
    }
  
    // Where the element to be moved f that 
    // index is less than 0 when 
    // 'moveToIndx += array size'
    while (moveToIndx < 0) 
    {
        moveToIndx = moveToIndx + arr.length;
    }
  
    // If 'moveToIndx' is greater than the 
    // size of the array then with need to 
    // push 'undefined' in the array.
    if (moveToIndx >= arr.length)
    {
        var un = moveToIndx - arr.length + 1;
        while (un--)
        {
            arr.push(undefined);
  
        }
    }
    // Here element of 'moveEle' is removed and 
    // pushed at 'moveToIndx' index
    arr.splice(moveToIndx, 0, arr.splice(moveEle, 1));
      
      
    // Print the array after moving
    document.write("<br>After move: "+arr+"<br>");
</script>

chevron_right


Output :

Original array: C++ ,Java ,JS ,Ruby ,Python

After move: C++ ,Ruby ,Java ,JS ,Python

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.