How to move specified number of elements to the end of an array in JavaScript ?
Last Updated :
28 Jun, 2024
The purpose of this article is to move some specified elements to the end of an array using JavaScript.
Given an array of length say N, move some specified amount of elements say X to the end of the given array.
Input:
arr = [1, 2, 3, 4, 5]
X = 2
Output: The following array should be the output as the first two numbers are moved to the end of the array.
[3, 4, 5, 1, 2]
Approach 1:
- First, we will extract first X elements from the array into a new array arr1.
- Then extract the last (N-X) elements from the array into a new array arr2.
- Then concatenate arr1 after arr2 to get the resulting array.
Example:
JavaScript
// arr is the input array and x is the no.
// of elements that needs to be moved to
// end of the array
function moveElementsToEndOfArray(arr, x) {
let n = arr.length;
// if x is greater than length
// of the array
x = x % n;
let first_x_elements = arr.slice(0, x);
let remaining_elements = arr.slice(x, n);
// Destructuring to create the desired array
arr = [...remaining_elements, ...first_x_elements];
console.log(arr);
}
let arr = [1, 2, 3, 4, 5, 6];
let k = 5;
moveElementsToEndOfArray(arr, k);
Output[ 6, 1, 2, 3, 4, 5 ]
Approach 2:
- Run a for loop from index i = 0 till X-1
- In each iteration take the element at the current index and append it at the end of the array.
- After the iteration is complete, use the JavaScript splice() method to remove the first X elements from the array to get the resultant array.
Example:
JavaScript
// Array is [1, 2, 3, 4, 5] and x = 2
// final output would be [3, 4, 5, 1, 2]
function moveElementsToEndOfArray(arr, x) {
x = x % (arr.length);
// After this loop array will
// be [1, 2, 3, 4, 5, 1, 2]
for (let i = 0; i < x; i++) {
arr.push(arr[i]);
}
// Splice method will remove first
// x = 2 elements from the array
// so array will be [3, 4, 5, 1, 2]
arr.splice(0, x);
console.log(arr);
}
let arr = [1, 2, 3, 4, 5];
let k = 2;
moveElementsToEndOfArray(arr, k);
Using push and shift in a Loop
The push and shift method in a loop moves elements from the start to the end. For a specified count, shift removes the first element and push appends it to the end of the array, effectively rotating the array.
Example: In this example This function moves count number of elements from the start of the array to the end using a loop, demonstrated with an example array and count value.
JavaScript
function moveElementsToEndWithLoop(arr, count) {
for (let i = 0; i < count; i++) {
arr.push(arr.shift());
}
return arr;
}
const array = [1, 2, 3, 4, 5];
console.log(moveElementsToEndWithLoop(array, 2));
Using Array Slice and Spread Operator
In this approach, we use the slice() method to divide the array into two parts: the first X elements and the remaining elements. We then use the spread operator to concatenate the two parts, with the first X elements placed at the end.
Syntax
const result = [...array.slice(X), ...array.slice(0, X)];
Example:
JavaScript
const arr = [1, 2, 3, 4, 5];
const X = 2;
const result = [...arr.slice(X), ...arr.slice(0, X)];
console.log(result); // Output: [3, 4, 5, 1, 2]
Similar Reads
How to remove specific elements from the left of a given array of elements using JavaScript ? In this article, we will learn How to remove specific elements from the left of a given array of elements using JavaScript. We have given an array of elements, and we have to remove specific elements from the left of a given array. Here are some common approaches: Table of Content Using splice() met
2 min read
How to move an array element from one array position to another in JavaScript? In JavaScript, we can access an array element as in 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
5 min read
How to Flatten a Given Array up to Specified Depth in JavaScript? Flatten an array in JavaScript is a common task when dealing with nested arrays. The concept of "flatten" means to convert a multi-dimensional array into a single-dimensional array. When you need to control the depth of flattening, JavaScript provides methods to handle this efficiently. This process
2 min read
How to convert array of strings to array of numbers in JavaScript ? Converting an array of strings to an array of numbers in JavaScript involves transforming each string element into a numerical value. This process ensures that string representations of numbers become usable for mathematical operations, improving data handling and computational accuracy in applicati
4 min read
How to Sort an Array Based on the Length of Each Element in JavaScript? Imagine you have a list of words or groups of items, and you want to arrange them in order from shortest to longest. This is a pretty common task in JavaScript, especially when working with text or collections of things. By sorting your list in this way, you can make sense of your data and make it e
3 min read