$.each 和 jQuery.inArray 函数组合的奇怪输出
我的完整代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所询问的代码的具体问题是您在检查之前将值转换为字符串:
inArray
执行===
(严格相等) 检查,然后"1" !== 1
。该行应为:更改:
不要将
值
转换为字符串。将结果与
-1
(数字)而不是"-1"
(字符串)进行比较。inArray
返回一个数字。使用
!==
而不是!=
(这主要是风格问题,如果您愿意,可以使用!=
) .不过,该代码还存在一些其他问题。
您缺少
}
,因此您所说的完整代码无法解析。每次调用
combinationCheck
时,您都会重新创建Combination
。如果您的目标是创建 Tic-Tac-Toe 游戏,您将需要能够在检查之间保留Combination
状态。这是一组相当少的修复:
...其中给出:
...报告未找到
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:
inArray
does an===
(strict equality) check, and"1" !== 1
. That line should read:Changes:
Don't turn the
value
into a string.Compare the result with
-1
(a number), not"-1"
(a string).inArray
returns a number.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.
You're missing a
}
, so the code you say is your complete code doesn't parse.You're re-creating
Combination
every time thecombinationCheck
is called. If your goal is to create a Tic-Tac-Toe game, you're going to need to be able to retain theCombination
state between checks.Here's a fairly minimal set of fixes:
...which given:
...reports that
1
is not found, but5
is.Live copy