Thursday, 19 March 2015

filter array from array with jquery


we have used jQuery.grep() method and jQuery.inArray() method to filter one array from another array.





<html>
<head>

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

<script>
$(document).ready(function(){

var OriginalArray = [ 1, 9, 3, 8, 6, 1, 5, 9, 4, 7, 3, 8, 6, 9, 1 ];
var arrayfilterElement = [ 1, 9, 3 ];


var FilteredArray;

$( "p.first" ).text( OriginalArray.join( ", " ) );

FilteredArray= jQuery.grep(OriginalArray, function( n, i ) {

return (jQuery.inArray(n,arrayfilterElement)==-1) ;

});

$( "p.last" ).text( FilteredArray.join( ", " ) );

});

</script>
</head>
<body>

<p class="first"></p>
<p class='last'></p>

</body>
</html>