According to the $.grep()
documentation I would think that the code below would print 2
. But instead it's printing 0,1,2
. See this fiddle. Am I going loco?
var arr = [0, 1, 2];
$.grep( arr, function(n,i) {
return n > 1;
});
$('body').html( arr.join() );
$.grep
returns a new array - it doesn't modify your existing one.
You want
arr = $.grep( arr, function(n,i) {
return n > 1;
});
Check out the $.grep docs for more information
You're missing a key part.
"Description: Finds the elements of an array which satisfy a filter function. The original array is not affected."
var newArray = $.grep( arr, function(n,i) {
return n > 1;
});
$('body').html( newArray.join() );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With