使用HTML元素匹配两个数组

时间:2017-03-03 01:30:34

标签: javascript arrays

我正在尝试仅在我的两个数组中的HTML匹配时才运行函数。尝试更改数组toString()会将数组中的HTML更改为“Object HTMLElement”,这不起作用。使用array.outerHTML()返回一个与array.val()相同的错误。我宁愿不做双循环,但我甚至尝试过,但仍然没有。这应该很简单,我错过了什么?

背景:我正在尝试制作一个西蒙游戏http://codepen.io/zjmitche/pen/MpWzop?editors=1010

//array content in console
var arrayOne = [section#three.square4, section#one.square4, section#three.square4, section#three.square4]
var arrayTwo = [section#three.square4, section#one.square4, section#three.square4, section#two.square4]

function nextCount() {
  if (arrayOne === arrayTwo) {
    //do something
  {
}

尝试循环:

for (var i = 0; i < arrayOne.length; i++) {
  for (var j = 0; j < arrayTwo.length; j++) {
    if (arrayOne[i] != arrayTwo[j]) {
      alert("test")
      arraysMatch = false;
    }
  }
}

1 个答案:

答案 0 :(得分:0)

嗯,首先,你只需要一个循环,因为如果长度不同,它们显然不匹配。然后,使用JSON.stringify轻松比较复杂值:

# Loop through the list until first hit.
for username, password in userPassList:  # Unpack the tuple as we fetch it.
  if username.startswith('*'):  # No mucking with indexes.
    self.conn...  # whatever
    break # We only need the first username