数字数组作为字符串到简单 int 转换问题

发布于 2024-12-02 02:04:22 字数 637 浏览 1 评论 0原文

控制台调试显示数组是 ex。 [“2”],但我需要[2]。

为什么铸造不起作用?

function filterElements(deals) {

    var result = deals,
        categories= $('#deals-categories').data('current_category');
        if (categories != undefined && categories.length > 0) {
            for (var i; i < categories.length; i++) {
                categories[i] = parseInt(categories[i]);
            }
            console.log(categories, 'cats');
                result = $.grep(result, function(e) {
                    return $.inArray(e.category_id, categories) != -1;
     });                
        }
    return result;
}

Console debug shows me that array is ex. ["2"], but I need [2].

Why casting doesnt'work?

function filterElements(deals) {

    var result = deals,
        categories= $('#deals-categories').data('current_category');
        if (categories != undefined && categories.length > 0) {
            for (var i; i < categories.length; i++) {
                categories[i] = parseInt(categories[i]);
            }
            console.log(categories, 'cats');
                result = $.grep(result, function(e) {
                    return $.inArray(e.category_id, categories) != -1;
     });                
        }
    return result;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧话新听 2024-12-09 02:04:22

您需要在循环声明中初始化var i = 0

完整代码清理:

function filterElements(deals) {

    var result = deals,
        categories = $('#deals-categories').data('current_category');

        if (categories && categories.length) {
            for (var i=0; i<categories.length; i++) {
                categories[i] = parseInt(categories[i], 10);
            }
            console.log(categories, 'cats');
            result = $.grep(result, function(e) {
                return $.inArray(e.category_id, categories) !== -1;
            });                
        }

    return result;
}

You need to initialize var i = 0 in the loop declaration.

Full code cleanup:

function filterElements(deals) {

    var result = deals,
        categories = $('#deals-categories').data('current_category');

        if (categories && categories.length) {
            for (var i=0; i<categories.length; i++) {
                categories[i] = parseInt(categories[i], 10);
            }
            console.log(categories, 'cats');
            result = $.grep(result, function(e) {
                return $.inArray(e.category_id, categories) !== -1;
            });                
        }

    return result;
}
墨离汐 2024-12-09 02:04:22

使用 categories[i] * 1 转换

parseInt 有时会以一种意想不到的方式工作:)

parseInt("010") 将返回 8一些浏览器,其他浏览器有 10 个:http://www.w3schools.com/jsref/jsref_parseInt.asp

use categories[i] * 1 to cast

parseInt works in a bit unexpected way sometimes :)

parseInt("010") will return 8 in some browsers, and 10 in others: http://www.w3schools.com/jsref/jsref_parseInt.asp

巷子口的你 2024-12-09 02:04:22

你确定吗?这是与您类似的示例:

var strings = ["1", "2", "3"];
var valueAsInt = 0;

for(var i = 0; i < strings.length; i++){
   valueAsInt = parseInt(strings[i]);
   if(typeof(valueAsInt) == 'number'){
      alert('Is an integer');
   }
}

消息“是整数”显示了三次。我认为在您的代码中解析器可以工作,但也许稍后,通过与其他字符串进行比较,或者可能进行一些串联,将该值转换为字符串。

Are you sure? This is an example similar to yours:

var strings = ["1", "2", "3"];
var valueAsInt = 0;

for(var i = 0; i < strings.length; i++){
   valueAsInt = parseInt(strings[i]);
   if(typeof(valueAsInt) == 'number'){
      alert('Is an integer');
   }
}

The message 'Is an integer' is shown three times. I think in your code the parser works, but maybe later, the value is converted to string by comparing with other string or, maybe some concatenation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文