How to Get all Elements Except First in JavaScript Array?
Last Updated :
23 Jan, 2025
Here are the various methods to get all elements except the first in JavaScript Array
1. Using for loop
We will use a for loop to grab all the elements except the first. We know that in an array the first element is present at index '0'.
JavaScript
const a1 = [1, 2, 3, 4, 5];
const a2 = [];
let k = 0;
for (let i = 1; i < a1.length; i++) {
a2[k] = a1[i];
k++;
}
console.log(a2);
In this example
- The code initializes an empty array find and a variable k for indexing.
- A for loop starts from the second element (i = 1) of the original array.
- Elements from the second position onward are copied to the find array.
- The result is printed without using a separate function.
2. Using slice() Method
The slice() is a method that returns a slice of an array. It takes two arguments, the start and end index.
JavaScript
const a = [10, 20, 30, 40, 50];
const res = a.slice(1);
console.log(res);
In this example
- The slice method is directly applied to the array a, starting from the second element (index 1).
- The result is stored in the result variable and printed, avoiding the use of a separate function.
3. Using Array.filter method
The Array.filter() method is used to create a new array with elements from the existing array which satisfies the condition.
JavaScript
const a = [10, 20, 30, 40, 50];
console.log(a.filter((x, y) =>
y != 0));
In this example
- The code uses the filter method to create a new array, excluding the element at index 0 (the first element) by checking if the index (y) is not equal to 0.
4. JavaScript Array.reduce() Method
The Array.reduce() is used to perform the function on each element of an array and form a single element.
JavaScript
const a = [10, 20, 30, 40, 50];
console.log(a.reduce((a, x, i) =>
(i == 0) ? a : a.concat(x), []));
In this example
- The code uses reduce to accumulate a new array, starting with an empty array, and skips the first element (index 0) by checking if the index is not equal to 0 before concatenating the remaining elements.
5. Using shift() Method
The JavaScript Array shift() Method removes the first element of the array thus reducing the size of the original array by 1.
JavaScript
let a = [34, 234, 567, 4];
a.shift();
console.log(a);
In this example
- The code uses shift to remove the first element from the array, and then logs the modified array, which no longer includes the first element.
6. Using Array.from() Method
The Array.from() method creates a new array instance from an array-like or iterable object.
JavaScript
const a = [10, 20, 30, 40, 50];
console.log(Array.from(a).slice(1));
In this example
- The Array.from method creates a shallow copy of the array, and slice(1) removes the first element, resulting in [20, 30, 40, 50].
7. Using splice() Method
The splice() method can be used to change the contents of an array by removing or replacing existing elements and/or adding new elements in place
JavaScript
const a = [10, 20, 30, 40, 50];
let res = a.slice();
res.splice(0, 1);
console.log(res);
In this example
- The slice() creates a shallow copy of the array, and splice(0, 1) removes the first element from the copied array, resulting in [20, 30, 40, 50].
8. Using Array.prototype.flatMap()
The flatMap()
method can be used creatively to ignore the first element of an array by mapping the array to a sub-array that excludes the first element and then flattening the result.
JavaScript
const a = [10, 20, 30, 40, 50];
const res = a.flatMap((_, index) =>
index === 0 ? [] : [a[index]]);
console.log(res);
In this example
- The flatMap method creates a new array by skipping the first element (index 0) and adding the remaining elements, resulting in [20, 30, 40, 50].
Similar Reads
JavaScript - How to Get First N Elements from an Array? There are different ways to get the first N elements from array in JavaScript.Examples:Input:arr = [1, 2, 3, 4, 5, 6], n = 3 Output: [1, 2, 3] Input:arr = [6, 1, 4, 9, 3, 5, 7], n = 4Output: [6, 1, 4, 9]1. Using slice() MethodThe slice() method is used to extract a part of an array and returns a new
4 min read
How to Check if an Element Exists in an Array in JavaScript? Given an array, the task is to check whether an element present in an array or not in JavaScript. If the element present in array, then it returns true, otherwise returns false.The indexOf() method returns the index of first occurance of element in an array, and -1 of the element not found in array.
2 min read
How to Filter an Array in JavaScript ? The array.filter() method is used to filter array in JavaScript. The filter() method iterates and check every element for the given condition and returns a new array with the filtered output.Syntaxconst filteredArray = array.filter( callbackFunction ( element [, index [, array]])[, thisArg]);Note: I
2 min read
JavaScript - How to Remove an Element from an Array? Removing elements from an array is a fundamental operation in JavaScript, essential for data manipulation, filtering, and transformation. This guide will explore different methods to efficiently remove elements from an array, enhancing your understanding and capability in handling arrays.1. Using po
3 min read
Remove Elements From a JavaScript Array Here are the various methods to remove elements from a JavaScript ArrayRemove elements from Array1. Using pop() methodThe pop() method removes and returns the last element of an array. This function decreases the length of the array by 1 every time the element is removed.javascriptlet a = ["Apple",
6 min read