How to compare javascript arrays and perform functions if similar

Hello, I am currently trying to compare two arrays containing ID numbers. If numbers in the first array are also in the second array, I want to change the style of each of the targeted element to have a background color of red. This is what I have so far:

var x = document.getElementsByTagName("td"); var len = x.length; var arr = []; for (var i=0; i<len; i++) { arr.push(x[i].id); if (arr.includes(<?php print_r(json_encode($winners_array))[0] ?> )) { x[i].style.color = "red"; } }

At the moment, I am trying to use the includes() method to compare the two arrays. This is the area that is not working. The PHP code that is json_encoded is just a PHP array I am sharing with JavaScript.

I appreciate all help! Thanks!

includes test if a value is present in the given array

an example of usage would be:

var array1 = [1, 2, 3];


And, if I got your question right, you are passing an array as argument to include, that may be the reason it’s not working as expected :+1:

EDIT: what you want to achieve is perhaps


ie: let's see if the current id is among winners_array ?

1 Like

Thanks for the clarification!