Home > Web Front-end > JS Tutorial > body text

How to find all but the first element in a given array using JavaScript?

WBOY
Release: 2023-09-06 23:13:02
forward
1333 people have browsed it

如何使用 JavaScript 查找给定数组中除第一个元素之外的所有元素?

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 -

Method 1: Use slice() method

The

slice() method is a JavaScript method used to slice the required number of elements from an array.

grammar

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
Copy after login

step

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.

Example

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>
Copy after login

Method 2. Use for loop

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.

step

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.

Example

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>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template