【问题标题】:weird output of the combination of $.each and jQuery.inArray function$.each 和 jQuery.inArray 函数组合的奇怪输出
【发布时间】:2012-02-23 00:38:10
【问题描述】:

我的完整代码:

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,它会提示“存在”,而 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.
});

我很困惑试图解决这个问题几个小时。

【问题讨论】:

  • 你为什么要将inArray 的结果与字符串进行比较?!
  • 另外,为什么要使用之后要立即覆盖的值来初始化数组?
  • 因为我正在编写井字游戏(与 PC 相比)。第一个玩家的位置将保存在一个数组中。并且此数组中的元素将使用组合进行检查。然后我进行组合,对其进行编程,以便 PC 响应并进行逻辑播放。
  • 能否分享您的完整代码。它对我有用。见jsfiddle.net/gApqm/4
  • @Freetalk13:恐怕你还没有理解我的两个cmets。 1、inArray的结果是一个数字;将其与数字而不是字符串进行比较。 2. 当您要立即使用a[0] = ...; a[1] = ...; a[2] = ...; a[3] = ...; 跟随它时执行a = [1, 2, 3, 4]; 是没有意义的,您正在覆盖用于初始化它的数据。只需使用a = []; 开始。或者更好的是,一次全部初始化:pastie.org/3288165

标签: jquery arrays each utilities


【解决方案1】:

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

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

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

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

变化:

  1. 不要将value 变成字符串。

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

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


不过,该代码还有几个其他问题。

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

  2. 你正在重新创建Combination每次combinationCheck 被调用。如果您的目标是创建井字游戏,则需要能够在两次检查之间保留 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

Live copy

【讨论】:

  • hmm.. 我试过了,但还是不适合我。我的代码和你写的一模一样
  • @Freetalk13: “我的代码和你写的一模一样” 显然不是,因为它确实有效(请参阅实时链接)。我唯一能想到的是,有一些与引用的代码无关的else,它根本无法运行。在 JavaScript 控制台中查找错误。
猜你喜欢
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 2012-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多