Compare Attributes of a List with Javascript

2

Hello, I have an array with li, I need to compare the values of the attributes, they all need to be the same .. example ..

I have 2 li within a ul

<li onclick="controle.selecionarVolta(this)" data-id="3" data-name="Rotina 3" data-criar="0" data-leitura="0" data-alterar="0" data-deletar="0" data-visitante="0" style="background: none;">Rotina 3</li>

<li onclick="controle.selecionarVolta(this)" data-id="3" data-name="Rotina 3" data-criar="0" data-leitura="0" data-alterar="0" data-deletar="0" data-visitante="1" style="background: none;">Rotina 2</li>

I need to check if all date values are the same as both ...

data-name, date-create, date-change, date-delete, and date-visitor.

All must contain the same value, if they have the same value, it returns true;

I'm getting these li's this way ..

var lista = document.querySelector('.rotinas_selecionadas').getElementsByTagName('li')

Then I have to go through this list, and check if all the data I said above are the same ...

Thank you in advance.

Sorry, I put it as an array, actually it's objects

    
asked by anonymous 12.01.2017 / 17:59

1 answer

0

The idea here is:

  • Store all lis values in an array
  • Verify that the first attribute of the first item matches that of the second
  • The criterion is that the attributes are in the same position, otherwise the comparison will go wrong

var lista = document.querySelectorAll('.rotinas_selecionadas')[0].getElementsByTagName('li');
var itens = [], igual = false;

for (let i = 0; i < lista.length; i++) {
  itens.push(lista[i].getAttribute("data-name"));
  itens.push(lista[i].getAttribute("data-criar"));
  itens.push(lista[i].getAttribute("data-alterar"));
  itens.push(lista[i].getAttribute("data-deletar"));
  itens.push(lista[i].getAttribute("data-visitante"));
}
for (let i = 0; i < itens.length - 5; i++) {
  if (itens[i] == itens[i+5]) igual = true;
  else igual = false;
}
console.log("É igual: " + igual);
<ul class="rotinas_selecionadas">
  <li onclick="controle.selecionarVolta(this)" data-id="3" data-name="Rotina 3" data-criar="0" data-leitura="0" data-alterar="0" data-deletar="0" data-visitante="0" style="background: none;">Rotina 3</li>

  <li onclick="controle.selecionarVolta(this)" data-id="3" data-name="Rotina 3" data-criar="0" data-leitura="0" data-alterar="0" data-deletar="0" data-visitante="1" style="background: none;">Rotina 2</li>
</ul>
    
12.01.2017 / 18:20