Iterate over an array and remove elements with splice() method - Javascript Array Operation

Javascript examples for Array Operation:Array Element

Description

Iterate over an array and remove elements with splice() method

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*  w  ww  .  ja  v a2s  . c  o  m*/
var elements = [1, 5, 5, 3, 5, 2, 4];
var remove = 5;
var index = elements.indexOf(remove);
while (index !== -1) {
    elements.splice(index, 1);
    index = elements.indexOf(remove);
}
console.log(elements);
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials