I'm using jquery.grep to clean a string and return only digits.
This is what I have:
var TheInputArray = TheInput.slice();
var TheCleanInput = jQuery.grep(TheInputArray, function (a) {
return parseInt(a, 10);
});
I take a string, split it into an array and use the parseInt function to check if it's a number. The problem is that when the value of a is 0, it skips that element. What changes do I need to do to make this code work?
Thanks.
I'm using jquery.grep to clean a string and return only digits.
This is what I have:
var TheInputArray = TheInput.slice();
var TheCleanInput = jQuery.grep(TheInputArray, function (a) {
return parseInt(a, 10);
});
I take a string, split it into an array and use the parseInt function to check if it's a number. The problem is that when the value of a is 0, it skips that element. What changes do I need to do to make this code work?
Thanks.
Share Improve this question edited Nov 16, 2011 at 23:44 Alex Wayne 187k52 gold badges325 silver badges357 bronze badges asked Nov 16, 2011 at 23:39 frenchiefrenchie 52k117 gold badges319 silver badges526 bronze badges6 Answers
Reset to default 5Unfortunately, 0
in Javascript is falsy. So you need to be sure your return value is true
, even for a 0
.
var TheInputArray = TheInput.slice();
var TheCleanInput = jQuery.grep(TheInputArray, function (a) {
return ! isNaN(parseInt(a, 10));
});
parseInt
returns NaN
(not a number), if it fails to parse the input. And isNan()
will return true
if the argument is NaN
. So this should help you detect that case.
You can use regular expressions:
var TheCleanInput = TheInput.replace(/\D/g, '');
If you just want to test whether the element in the array is a number, rather than converting it and then building a new array using the converted numbers, you can use the new $.isNumeric
function (new in 1.7), which tests whether the argument represents a numeric value:
var TheCleanInput = jQuery.grep(TheInputArray, function (a) { return $.isNumeric(a); });
Note that this does not modify the existing array. If the array contains '5'
, that will remain a string and not be converted to a number. Use $.map
if that's what you want.
The parseInt()
function doesn't return a boolean value, it either returns NaN
for non-numeric values or the converted value for numeric values. If you try to use the result as a boolean you'll find that NaN
and 0 will both be falsy, while any non-zero number will be equivalent to true.
You can use isNan()
to check this: return !isNan(parseInt(a,10));
Or you can use jQuery's $.isNumeric(a)
function instead (if using jQuery 1.7+).
Or if you just want to remove all non-numeric characters from a string why not use a regex replace:
TheInput.replace(/\D/g,"")
Even if you specifically want the result as an array I think you're best off using the regex and then converting to an array afterwards because it keeps the code simple.
If you're using the current Version of jQuery (1.7), you can use jQuery.isNumeric(a)
for that purpose:
var TheCleanInput = jQuery.grep(TheInputArray, function (a) { return $.isNumeric(a); });
See https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
var filterInt = function(value) {
if (/^(\-|\+)?([0-9]+|Infinity)$/.test(value))
return Number(value);
return NaN;
}