Open In App

How to find all elements in a given array except for the first one using JavaScript ?

Last Updated : 03 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

To find all elements in a given array except for the first one using JavaScript, we have multiple approaches, In this article, we will learn how to find all elements in a given array except for the first one.

These are the multiple methods by which we can find all elements in a given array except for the first one

Method 1: Using 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 a 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
}
// An input array
const arr = [1, 2, 3, 4, 5]
 
// Printing the result
console.log(print(arr))


Output

[ 2, 3, 4, 5 ]

Method 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.

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 ]

Method 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.

Example: In this example, we are using shift() Method.

Javascript




function func() {
 
    // Original array
    let array = [34, 234, 567, 4];
 
    // Checking for condition in array
    let value = array.shift();
    console.log(array);
}
func();


Output

[ 234, 567, 4 ]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads