Removing an element of an array with splice - Javascript Array Operation

Javascript examples for Array Operation:Array Element

Description

Removing an element of an array with splice

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width"> 
   </head> 
   <body> 
      <script>
my =["1", "2", "3", "4", "5", "6"];

function removeFromArray(array, el) {
   var pos = array.indexOf(el);/*from  w w w . ja v  a2s  .  c o m*/
   pos > -1 && array.splice(pos, 1);
}
removeFromArray(my, "2");
console.log(my);
removeFromArray(my, "5");
console.log(my);

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

Related Tutorials