Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

110
Visualizações
finding all matches and returning values from array object
var sel = document.querySelectorAll('ul li a')
sel[0] == // ELEMENT WITH div.bottom ul li a.box
sel[1] == // ELEMENT WITH div.head ul li a.box

var list = [
    {css: 'a span', val:'1st'},
    {css: 'div.bottom ul a', val:'2nd'},
    {css: 'div.head li > a.box', val:'3rd'},
    {css: 'div.head li a', val:'4th'}
]

//desired result 
"sel[0] matches 2nd"
"sel[1] matches 3rd,4th"

I'm trying to find all css selectorAll matches from a list. I know I can use nested for loop and loop through both selectors and list in this way

for (var i = 0; i < list.length; i++) {
    if (sel[0].matches(list[i].css)) {
      console.log(list[i].val);
    }
  }

but trying to find better solution without using nested for loop. How can I improve this code?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Use the filter() method to get all matching elements of an array.

sel.forEach((el, i) => {
    let matches = list.filter(item => el.matches(item.css));
    if (matches.length > 0) {
        console.log(`sel[${i}] matches ${matches.map(item => item.val).join(',')}`);
    }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda