Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»JavaScript»How to Remove Array Element by Value in JavaScript

    How to Remove Array Element by Value in JavaScript

    RahulBy RahulNovember 6, 20151 Min ReadUpdated:May 6, 2020

    The best way to remove an element from an array based on the value in JavaScript is to find index number of that value in an array using indexOf() function and then delete particular index value using the splice() function. For example use following code.

    Sample JavaScript Code

    <script type="text/JavaScript">
    
        var arr = [5, 15, 110, 210, 550];
        var index = arr.indexOf(210);
    
        if (index > -1) {
           arr.splice(index, 1);
        }
    
    </script>
    

    Explanation:

    • Line 1 – We have initialized an array named arr with few static values.
    • Line 2 – Now we use indexOf() function to find the index number of given value in array. If given value found in array , it will return index number, else it will remove values less than 0.
    • Line 3 – First check if return index number is >=0, then only delete the value from that index from array using splice() function.
    array javascript js
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleInitial Server Setup for Beginners (Ubuntu 18.04 and 16.04 LTS)
    Next Article How to Loop Over an Array in JavaScript

    Related Posts

    5 Methods to Print an Array in Java

    Updated:April 21, 20222 Mins Read

    JavaScript Program to Add Two Numbers

    2 Mins Read

    How to Install ReactJS on Ubuntu 20.04

    3 Mins Read

    Remove First Character from String in JavaScript

    1 Min Read

    10 JavaScript Console Methods

    Updated:November 14, 20203 Mins Read

    How to Check If a Value Exists in An Array in PHP

    1 Min Read

    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

    Recent Posts
    • How to Install Sublime Text 4 on Ubuntu 22.04
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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