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


In this tutorial, we are going to find all the elements present in the given array except the first element using JavaScript. Here we will create one array and using JavaScript will print all the elements on the HTML page leaving the first element.

There are mainly two approaches we can use to do the above task. Both the methods are given as −

Approach 1: By using the slice() Method

The slice() method is a JavaScript method that is used to slice out the required amount of elements from the array.

Syntax

The following syntax can be used with one argument value also in which it will trim all the elements in the array after the given index −

const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const citrus = fruits.slice(1); //   Orange, Lemon, Apple, Mango

Steps

Steps to produce the required result using the slice() method.

Step 1 − Create an array filled with some elements.

Step 2 − Create a function that takes an array as an argument.

Step 3 − In this step, we will create a variable that stores the result from the array.slice() method.

Step 4 − Return the result variable from the function.

Step 5 − We call the function to give the result.

Example

We can use the below code to find all elements in a given array except for the first one using slice() method −

<!DOCTYPE html> <html> <head> <h2> Tutorials Point </h2> </head> <body> <script> const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; const remove_first = (fruits) => { const result = fruits.slice(1) return result } let output = remove_first(fruits) document.write(output) </script> </body> </html>

Approach 2. By using the for loop

In this approach, we are going to run a for loop through the array and print all the elements of the array except the first one. We are using the logic of index number here as we know the index number of an array starts from 0 so we run the loop through index 1.

Steps

Steps to produce the required result using for loop −

Step 1 − Create an array filled with some elements in it.

Step 2 − Create a function that takes an array as an argument.

Step 3 − In this step, we will create an empty array to store the elements we loop through for loop and also create a variable to keep the length of the new array.

Step 4 − Create a for loop which is looping through the original array from index number i=1 up to the length of an array.

Step 5 − Store the elements of the original array into the new array created above.

Step 6 − Return the resultant array from the function

Step 7 − We call the function to give the result.

Example

We can use the below code to find all elements in a given array except for the first one using slice() method −

<!DOCTYPE html> <html> <head> <h2> Tutorials Point </h2> </head> <body> <script> const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; const remove_first = (fruits) => { const new_arr = [] let a = 0 for (let i = 1; i < fruits.length; i++) { new_arr[a] = fruits[i] a++ } return new_arr } let output = remove_first(fruits) document.write(output) </script> </body> </html>

So from this article we got to learn the two methods used to find all elements in a given array except for the first one. We can use these given methods whenever we want to trim the elements from the array. The methods can also be used when we are required to remove more than one elements from the array we just need to change the index number in the slice() method and in the for loop.

Updated on: 18-Oct-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements