te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>jquery - javascript parseInt when value = 0 - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - javascript parseInt when value = 0 - Stack Overflow

programmeradmin4浏览0评论

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 badges
Add a ment  | 

6 Answers 6

Reset to default 5

Unfortunately, 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;
}
发布评论

评论列表(0)

  1. 暂无评论