Remove values from an array that are in a second array with filter function - Javascript Array

Javascript examples for Array:indexOf

Description

Remove values from an array that are in a second array with filter function

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(){//from   ww w .  j a  va  2  s  .  c  o  m
var array1 = [ 1, 2, 3, 4, 5 ];
var array2 = [ 2, 3 ];
var result = array1.filter( function ( elem ) {
    return array2.indexOf( elem ) === -1;
});
document.write( result );
    }

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

Related Tutorials