如何检查充满Jquery对象的数组的值

时间:2018-06-27 20:10:10

标签: javascript jquery html loops

我正在做一个井字游戏,我想做一些不同的事情。

我目前正在尝试通过迭代由Jquery抓取的存储tds数组来检查获胜组合。

WIN_COMBINATIONS = [$("#square_0, #square_1, #square_2"), 
$("#square_6, #square_7, #square_8"),
$("#square_0, #square_3, #square_6"), 
$("#square_3, #square_4, #square_5"), 
$("#square_1, #square_4, #square_7"),
$("#square_2, #square_5, #square_8"), 
$("#square_0, #square_4, #square_8"), $("#square_6, #square_4, #square_2")]

因此,基本上,WIN_COMBINATIONS [0]是赢家。迭代并实际检查Jquery对象的.html的最佳方法是什么?

基本上,我想做这样的事情

if (WIN_COMBINATIONS[0].html = "X", "X", "X") {
        //do something here
}

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

var languages = GetSiteLanguages();
foreach (var lang in languages)
{
    lstLanguages.Items.Add(new ListItem(lang.Name));
}
lstLanguages.SelectionMode = ListSelectionMode.Multiple;

答案 1 :(得分:0)

如果ES6(ES2015)正常,则可以尝试reduce to find match

!!array.reduce(function(a, b){ return (a === b) ? a : NaN; });

结果:

var array = ["a", "a", "a"] => result: "true"
var array = ["a", "b", "a"] => result: "false"
var array = ["false", ""] => result: "false"
var array = ["false", false] => result: "false"
var array = ["false", "false"] => result: "true"
var array = [NaN, NaN] => result: "false" 

警告: var array = [] => result: TypeError thrown

所有功劳归给: Lightness Races in Orbit