There are two arrays that need to be compared.

var arr1 = document.querySelectorAll('#items p'); //Первый массив var arr2 = [1,2,3,4,5] //Второй массив 
 <ul id="items"> <li><p>1</p></li> <li><p>2</p></li> <li><p>3</p></li> <li><p>4</p></li> <li><p>5</p></li> </ul> 

It is necessary to check whether the values ​​of the paragraphs coincide with the array, it is impossible to pull out the value of p, so I can not compare.

I entered the same values ​​for the example, the values ​​are substituted all the time different.

  • one
    Compare to what? - Misha Saidov
  • It is necessary to check whether the values ​​of the paragraphs coincide with the array, it is impossible to pull out the value of p, so I can not compare - Rinat Arifullin
  • I entered the same values ​​for example, the values ​​are substituted all the time different - Rinat Arifullin

2 answers 2

An example with full compliance:

 var arr1 = document.querySelectorAll('#items p'); //Первый массив var arr2 = [1, 2, 3, 4, 5] //Второй массив if (arr1.length === arr2.length) { for (var i = 0; i < arr1.length; i++) { if (+arr1[i].textContent === arr2[i]) { console.log('Значение ' + arr1[i].textContent + ' в массиве arr1 равно значению ' + arr2[i].toString() + ' в массиве arr2') } else if (+arr1[i].textContent !== arr2[i]) { console.log('Значение ' + arr1[i].textContent + ' в массиве arr1 не равно значению ' + arr2[i].toString() + ' в массиве arr2'); } } } 
 <ul id="items"> <li> <p>1</p> </li> <li> <p>2</p> </li> <li> <p>3</p> </li> <li> <p>4</p> </li> <li> <p>5</p> </li> </ul> 

Example with mismatch of elements:

 var arr1 = document.querySelectorAll('#items p'); //Первый массив var arr2 = [1, 2, 3, 4, 5] //Второй массив if (arr1.length === arr2.length) { for (var i = 0; i < arr1.length; i++) { if (+arr1[i].textContent === arr2[i]) { console.log('Значение ' + arr1[i].textContent + ' в массиве arr1 равно значению ' + arr2[i].toString() + ' в массиве arr2') } else if (+arr1[i].textContent !== arr2[i]) { console.log('Значение ' + arr1[i].textContent + ' в массиве arr1 не равно значению ' + arr2[i].toString() + ' в массиве arr2'); } } } 
 <ul id="items"> <li> <p>1</p> </li> <li> <p>3</p> </li> <li> <p>4</p> </li> <li> <p>4</p> </li> <li> <p>5</p> </li> </ul> 

PS You can also make a check for types

  • All, thank you very much - Rinat Arifullin
  • @ RinatArifullin, not at all, I wish you success! - meine

 function check(ps, vals) { if (ps.length !== vals.length) { return false; } for (var q=0; q<vals.length; ++q) { if (ps[q].textContent != vals[q]) { return false; } } return true; } console.log(check( document.querySelectorAll('#items p'), [1,2,3,4,5] )); 
 <ul id="items"> <li><p>1</p></li> <li><p>2</p></li> <li><p>3</p></li> <li><p>4</p></li> <li><p>5</p></li> </ul>