Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»JavaScript»How to Remove JavaScript Array Element by Value

    How to Remove JavaScript Array Element by Value

    By RahulJune 9, 20222 Mins Read

    An array is a collection of values referenced by the array name. Once the array is initialized, we can easily add more elements or delete existing elements from an array. In this tutorial, you will learn to remove array elements by value in JavaSscript.

    Advertisement

    We can use the following JavaScript methods to remove an array element by its value.

    • indexOf() – function is used to find array index number of given value. Return negavie number if the matching element not found.
    • splice() function is used to delete a particular index value and return updated array.

    Remove Array Element in JavaScript

    Use the indexOf() and splice() function to remove an array elements by its value. Let’s understand with an example:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <script type="text/JavaScript">
        // Initialize array
        var arr = [5, 15, 110, 210, 65, 210];
        console.log(arr);
     
        // Get the index of element
        var index = arr.indexOf(210);
     
        if (index > -1) {
           arr.splice(index, 1); // Remove array element
        }
        console.log(arr);
    </script>

    The second value in splice() function tells to delete number of elements starting from index in array. See the below screenshot of the execution at playcode:

    Remove Elements in Array by Value in JavaScript
    Remove one array element by value.

    Did you notice that only the first occurrence of the matching element is deleted from the array? It’s because the indexOf() returns the single index number of the first matching value. To delete all the occurrences of the matching element use while loop. Let’s see an example:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <script type="text/JavaScript">
        // Initialize array
        var arr = [5, 15, 110, 210, 65, 210];
        console.log(arr);
     
        var i = 0;
        while (i < arr.length) {
          if (arr[i] === 210) {
             arr.splice(i, 1);
          } else {
             ++i;
          }
        }
     
        console.log(arr);
    </script>

    Run this script again and see the results:

    Remove All Matching Elements in Array by Value - JavaScript
    Remove all matching array elements in array by value

    You will find that all the matching elements are deleted from the defined array.

    Wrap Up

    In this tutorial, you have learned about removing array elements by value in JavaScript.

    If you like this tutorial, please share it with your social media handles. Also, provide your valuable suggestions in the comment box to improve this answer.

    array javascript js
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to list all collections in MongoDB database

    What is difference between var, let and const in JavaScript?

    What is difference between var, let and const in JavaScript?

    How do I Replace String in JavaScript

    How to Replace String in JavaScript

    View 6 Comments

    6 Comments

    1. Leo on August 29, 2020 7:54 pm

      arr = arr.filter(x=>x!=2)

      Reply
    2. Rathnayake Ranga on June 1, 2020 12:20 am

      Thank You Very Much

      Reply
    3. Pedro on September 25, 2019 7:21 pm

      Thanks for this code, this code helped me, Very good!

      Reply
    4. JavaScript Dude on March 26, 2019 1:48 am

      This is a great example of how to use Splice(). Thank you for sharing.

      Reply
    5. Matt on October 24, 2018 3:03 pm

      Delete all occurrences of a specified value from an array:

      var arr = [100,2,500,3,2,240,35,2];

      removedIndx = arr.indexOf(2);
      while(removedIndx > -1) {
      arr.splice(removedIndx,1);
      removedIndx = arr.indexOf(2);
      }

      Reply
    6. Xcapee on August 14, 2017 5:02 am

      For practical purposes, you probably want that in a while loop. A “delete by value” function should logically delete all occurrences of that value.

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Split Large Archives in Linux using the Command Line
    • System.out.println() Method in Java: A Beginner’s Guide
    • Split Command in Linux With Examples (Split Large Files)
    • Test Your Internet Speed from the Linux Terminal
    • 11 Practical Example of cat Command in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.