$.each 和 jQuery.inArray 函数组合的奇怪输出

发布于 2024-12-31 23:00:26 字数 1069 浏览 1 评论 0原文

我的完整代码:

jQuery.extend({

combinationCheck: function (p1position) {

    var Combination = [1, 2, 3, 4, 5, 6, 7, 8];
    Combination[0] = [1, 2, 3];
    Combination[1] = [4, 5, 6];
    Combination[2] = [7, 8, 9];
    Combination[3] = [1, 4, 7];
    Combination[4] = [2, 5, 8];
    Combination[5] = [4, 6, 8];
    Combination[6] = [1, 5, 9];
    Combination[7] = [3, 5, 7];


    $.each(p1position, function (index, value) {

        var num = value;

        if ($.inArray(String(value), Combination[1]) != '-1') {
            alert("there");
        }
        else {
            alert("not there");
        }

    });
});

所以它有效。如果我将 num 设置为 5,它会发出警报“is There”,如果我将 num 设置为 8 --> ,则会发出警报。 “不在那里”。 但问题是我有另一个数组。

p1position = [1,5];

并遍历数组..

$.each(p1position,function(index,value){
    var num = value;
//then call the jQuery.inArray function as written above, it always return not there. even though 5 is in the Combination[1] array.
});

我对花几个小时试图解决这个问题感到非常困惑。

My complete code:

jQuery.extend({

combinationCheck: function (p1position) {

    var Combination = [1, 2, 3, 4, 5, 6, 7, 8];
    Combination[0] = [1, 2, 3];
    Combination[1] = [4, 5, 6];
    Combination[2] = [7, 8, 9];
    Combination[3] = [1, 4, 7];
    Combination[4] = [2, 5, 8];
    Combination[5] = [4, 6, 8];
    Combination[6] = [1, 5, 9];
    Combination[7] = [3, 5, 7];


    $.each(p1position, function (index, value) {

        var num = value;

        if ($.inArray(String(value), Combination[1]) != '-1') {
            alert("there");
        }
        else {
            alert("not there");
        }

    });
});

so it works. If I were to set num to 5, it alerts "is there", and for 8 --> "not there".
but the problem is I have another array.

p1position = [1,5];

and go through the array..

$.each(p1position,function(index,value){
    var num = value;
//then call the jQuery.inArray function as written above, it always return not there. even though 5 is in the Combination[1] array.
});

I am so confused of trying to solve this problem for hours.

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

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

发布评论

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

评论(1

临走之时 2025-01-07 23:00:26

您所询问的代码的具体问题是您在检查之前将值转换为字符串:

if ($.inArray(String(value), Combination[1]) != '-1') {
//            ^^^^^^^^^^^^^

inArray 执行 === (严格相等) 检查,然后"1" !== 1。该行应为:

if ($.inArray(value, Combination[1]) !== -1) {

更改:

  1. 不要将 转换为字符串。

  2. 将结果与 -1(数字)而不是 "-1"(字符串)进行比较。 inArray 返回一个数字。

  3. 使用 !== 而不是 != (这主要是风格问题,如果您愿意,可以使用 !=) .


不过,该代码还存在一些其他问题。

  1. 您缺少 },因此您所说的完整代码无法解析。

  2. 每次调用 combinationCheck 时,您都会重新创建Combination。如果您的目标是创建 Tic-Tac-Toe 游戏,您将需要能够在检查之间保留 Combination 状态。

这是一组相当少的修复:

(function() {
    var Combination = [
            [1, 2, 3],
            [4, 5, 6],
            [7, 8, 9],
            [1, 4, 7],
            [2, 5, 8],
            [4, 6, 8],
            [1, 5, 9],
            [3, 5, 7]
        ];

    jQuery.extend({

        combinationCheck: function (p1position) {

            $.each(p1position, function (index, value) {

                if ($.inArray(value, Combination[1]) !== -1) {
                    alert(value + " is there");
                }
                else {
                    alert(value + " is NOT there");
                }

            });
        }
    });

})();

...其中给出:

jQuery.combinationCheck([1, 5]);

...报告未找到 1,但找到 5

实时副本

The specific problem with your code that you're asking about is that you're turning the value into a string before the check:

if ($.inArray(String(value), Combination[1]) != '-1') {
//            ^^^^^^^^^^^^^

inArray does an === (strict equality) check, and "1" !== 1. That line should read:

if ($.inArray(value, Combination[1]) !== -1) {

Changes:

  1. Don't turn the value into a string.

  2. Compare the result with -1 (a number), not "-1" (a string). inArray returns a number.

  3. Use !== rather than != (this is mostly a matter of style, you can use != if you prefer).


There are several other problems with that code, though.

  1. You're missing a }, so the code you say is your complete code doesn't parse.

  2. You're re-creating Combination every time the combinationCheck is called. If your goal is to create a Tic-Tac-Toe game, you're going to need to be able to retain the Combination state between checks.

Here's a fairly minimal set of fixes:

(function() {
    var Combination = [
            [1, 2, 3],
            [4, 5, 6],
            [7, 8, 9],
            [1, 4, 7],
            [2, 5, 8],
            [4, 6, 8],
            [1, 5, 9],
            [3, 5, 7]
        ];

    jQuery.extend({

        combinationCheck: function (p1position) {

            $.each(p1position, function (index, value) {

                if ($.inArray(value, Combination[1]) !== -1) {
                    alert(value + " is there");
                }
                else {
                    alert(value + " is NOT there");
                }

            });
        }
    });

})();

...which given:

jQuery.combinationCheck([1, 5]);

...reports that 1 is not found, but 5 is.

Live copy

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