How to find all elements in a given array except for the first one using JavaScript ?
In this article, we will learn how to find all elements in a given array except for the first one. There are multiple methods:
- JavaScript for loop
- JavaScript slice() Method
- Using Array.filter method
- JavaScript Array.reduce() Method
Method 1: JavaScript for loop: In this method, 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’. We will run a loop from 1 to array.length and save those remaining elements to another array.
Example: In this example, we will be using for loop to find all elements in an array except for the first element.
Javascript
//Javascript program to find all //element of an array except first //function which takes an array as argument const print = (arr) => { //creating a dummy array to store result const find = [] //a counter for adding result let k = 0 for (let i = 1; i < arr.length; i++) { find[k] = arr[i] k++ } //returning resultant array return find } //input array const arr = [1, 2, 3, 4, 5] //printing the result console.log(print(arr)) |
Output:
[ 2, 3, 4, 5 ]
Method 2: JavaScript slice() Method: The slice() is a method that returns a slice of an array. It takes two arguments, the start and end index.
Example: In this example, we will be using the slice() method to find all elements in an array except for the first element.
Javascript
//Javascript program to find all //element of array except first //function which takes an array as argument const print = (arr) => { //returning resultant array return arr.slice(1) } //input array const arr = [10, 20, 30, 40, 50] //printing the result console.log(print(arr)) |
Output :
[20, 30, 40, 50]
Method 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.
Example: In this example, we will be using the filter() method to find all elements in an array except for the first element.
Javascript
//Javascript program to find all //element of array except first //function which takes an array as argument const print = (arr) => { //returning resultant array return arr.filter((x, y) => y != 0) } //input array const arr = [10, 20, 30, 40, 50] //printing the result console.log(print(arr)) |
Output:
[20, 30, 40, 50]
Example 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.
Example: In this example, we will be using the reduce() method to find all elements in an array except for the first element.
Javascript
//Javascript program to find all //element of array except first //function which takes an array as argument const print = (arr) => { //returning resultant array return arr.reduce((a, x, i) => (i == 0) ? a : a.concat(x), []) } //input array const arr = [10, 20, 30, 40, 50] //printing the result console.log(print(arr)) |
Output:
[ 20, 30, 40, 50 ]
Please Login to comment...