简体   繁体   English

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

[英]How to check the values of an array full of Jquery objects

I am making a tic-tac-toe game for fun and I am trying to do something a little different. 我正在做一个井字游戏,很有趣,并且我尝试做一些不同的事情。

I am currently trying to check winning combinations by iterating through an array of stored tds that were grabbed with Jquery. 我目前正在尝试通过遍历用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")]

So, basically, WIN_COMBINATIONS[0] is a winning combo. 因此,基本上,WIN_COMBINATIONS [0]是一个成功的组合。 What is the best way to iterate through, and actually check the .html of the Jquery object? 迭代并检查Jquery对象的.html的最佳方法是什么?

Basically, I would like to do something like this 基本上,我想做这样的事情

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

Thanks for your help! 谢谢你的帮助!

WIN_COMBINATIONS.forEach(function(combination){
    if(combination.map(function(){return $(this).text()}).toArray().join("") == "XXX") { 
        console.log("winning combination")   
    } 
})   

if ES6 (ES2015) is OK than you can try reduce to find match 如果ES6(ES2015)正常,则可以尝试减少以找到匹配项

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

Results: 结果:

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" 

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

All credit to: Lightness Races in Orbit 全部归功于: 轨道轻度竞赛

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM