简体   繁体   English

forEach 里面的 forEach 有条件吗?

[英]forEach inside forEach with a condition?

I have two lists and I want to check if list B contains items from list A, so I have created a button that triggers a forEach to check if any of list B's elements' innerText is similar to the ones in list A:我有两个列表,我想检查列表 B 是否包含列表 A 中的项目,因此我创建了一个按钮来触发 forEach 以检查列表 B 的任何元素的 innerText 是否与列表 A 中的元素相似:

    let itemsFromA = document.querySelectorAll('.itemA')
    let listB = document.getElementById('listB')

    listB.childNodes.forEach(childNodes => {
        if (childNodes.innerText === itemsFromA.forEach(itemsFromA => itemsFromA.innerText)) {
            childNodes.style.display = 'none'
        }
    })
}

But nothing happens.但什么也没有发生。 How can I fix this?我怎样才能解决这个问题?

Thanks谢谢

NodeList.forEach() doesn't return anything (or always returns undefined ). NodeList.forEach()不返回任何内容(或始终返回undefined )。 Use Array.from() to convert the itemsFromA to an array, and Array.some() to check if any item has the same innerText :使用Array.from()itemsFromA转换为数组,并使用Array.some()检查是否有任何项目具有相同的innerText

let itemsFromA = document.querySelectorAll('.itemA')
let listB = document.getElementById('listB')

listB.childNodes.forEach(node => {
  if (Array.from(itemsFromA).some(itemsFromA => itemsFromA.innerText === node.innerTex)) {
    node.style.display = 'none'
  }
})

You can use some instead of forEach (which doesn't return anything, hence your code isn't working..).您可以使用some而不是forEach (它不返回任何内容,因此您的代码不起作用..)。

some will test whether or not at least one element in the array passes the test implemented by the provided function.有些人会测试数组中的至少一个元素是否通过了提供的 function 实现的测试。

let itemsFromA = document.querySelectorAll('.itemA')
let listB = document.getElementById('listB')

new_list = listB.childNodes.map((index, childNodes) => {
    if (itemsFromA.some(item => item.innerText === childNodes.innerText)) {
        childNodes.style.display = 'none'
    }
})

This is because forEach loop doesn't return values.这是因为forEach循环不返回值。 Use map instead of forEach for expected behaviour.使用 map 而不是 forEach 来获得预期的行为。

eg:例如:

if (childNodes.innerText === itemsFromA.map(itemsFromA => itemsFromA.innerText))

Try this way:试试这个方法:

  let itemsFromA = document.querySelectorAll('.itemA')
  let listB = document.getElementById('listB')

  list = listB.childNodes.map((index, childNodes) => {
      itemsFromA.forEach(itemsFromA => {
        if (childNodes.innerText === itemsFromA.innerText)
           childNodes.style.display = 'none'
      })
    })
}

forEach does not return any value, it will only loop through the array. forEach 不返回任何值,它只会循环遍历数组。 you should use find function instead你应该改用 find function

`let itemsFromA = document.querySelectorAll('.itemA') let listB = document.getElementById('listB') `让 itemsFromA = document.querySelectorAll('.itemA') 让 listB = document.getElementById('listB')

listB.childNodes.forEach(childNode => {
    if (itemsFromA.find(itemsFromA => itemsFromA.innerText == childNode.innerText)) {
        childNodes.style.display = 'none'
    }
})

}` }`

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

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