I need to see if an element is in another list or not but they are not in order and they are not equal size.
let list1 = [10 , 4 , 5]
let list 2 = [4 , 5]
I tried this but this only works if they are in same order.
for(var i=0;i<=document.querySelectorAll('.datename').length;i++){
if(document.querySelectorAll('.inputname')[i].value==document.querySelectorAll('.datename')[i].innerHTML){
console.log(document.querySelectorAll('.other')[c].classList)
document.querySelectorAll('.other')[c].classList.add('active2')
}
}
2
Answers
You can put all the element values into an array first, then use
Array#includes
to check for a match.The possible solution you have tried is:
Or,
If you want to check if data of an array contains in another array and if they are not same length then you can use the
include()
method.the output will be:
//Element 4 is in list1.
//Element 5 is in list1.
check the sandbox: https://codesandbox.io/embed/sleepy-stitch-jb3c6l?fontsize=14&hidenavigation=1&theme=dark