In this tutorial, we will find all but the first element in a given array using JavaScript. Here we will create an array and use JavaScript to print all the elements on the HTML page, leaving only the first element.
We can mainly use two methods to complete the above tasks. Both methods are given as -
slice() method is a JavaScript method used to slice the required number of elements from an array.
The following syntax can be used with a parameter value where it will trim all elements after the given index in the array -
const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; const citrus = fruits.slice(1); // Orange, Lemon, Apple, Mango
Steps to use the slice() method to produce the desired result.
Step 1 - Create an array filled with some elements.
Step 2 - Create a function that takes an array as a parameter.
Step 3 - In this step, we will create a variable to store the result of array.slice() method.
Step 4 - Return the result variable from the function.
Step 5 - We call the function to give the result.
We can use the following code to find all elements except the first element in a given array using the 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>
In this method we will run a for loop in the array and print all the elements in the array except the first element. We use index number logic here because we know that the index numbers of the array start at 0, so we run the loop through index 1.
Steps to use a for loop to generate the desired results -
Step 1 - Create an array filled with some elements.
Step 2 - Create a function that takes an array as a parameter.
Step 3 - In this step, we will create an empty array to store the elements looped through in the for loop and create a variable to hold the length of the new array.
Step 4 - Create a for loop that loops through the original array from index number i=1 to the length of the array.
Step 5 - Store the elements of the original array into the new array created above.
Step 6 - Return the result array from the function
Step 7 - We call the function to give the result.
We can use the following code to find all elements except the first element in a given array using the 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 learned two methods for finding all elements except the first element in a given array. Whenever we want to prune we can use these given methods elements in the array. These methods can also be used when we need to remove multiple elements from the array, we just need to change the index number in the slice() method and for loop.
The above is the detailed content of How to find all but the first element in a given array using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!