英文:
comparing each item in a list to another one
问题
我需要查看一个元素是否在另一个列表中,但它们的顺序不同,并且它们的大小也不相等。
let list1 = [10, 4, 5];
let list2 = [4, 5];
我尝试了以下代码,但这只适用于它们具有相同的顺序:
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');
}
}
英文:
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')
}
}
答案1
得分: 0
你可以先将所有元素的值放入一个数组中,然后使用 Array#includes
来检查是否有匹配项。
let vals = Array.from(document.querySelectorAll('.inputname'), el => el.value);
for (const el of document.querySelectorAll('.datename')) {
if (vals.includes(el.textContent)) {
// 做一些操作
}
}
英文:
You can put all the element values into an array first, then use Array#includes
to check for a match.
let vals = Array.from(document.querySelectorAll('.inputname'), el => el.value);
for (const el of document.querySelectorAll('.datename')) {
if (vals.includes(el.textContent)) {
// do something
}
}
答案2
得分: 0
您尝试的可能解决方案是:
const input = document.querySelectorAll('.input');
const data = document.querySelectorAll('.data');
for (let i = 0; i < input.length; i++) {
const inputValue = input[i].value;
if (Array.from(data).some(dateElement => dateElement.innerHTML === inputValue)) {
console.log('Element ' + inputValue + ' is included in datename list.');
// 如果元素包含在内,执行某些操作,例如:
document.querySelectorAll('.other')[i].classList.add('active2');
} else {
console.log('Element ' + inputValue + ' is not included in datename list.');
}
}
或者,
如果您想检查一个数组的数据是否包含在另一个数组中,且它们的长度不相等,您可以使用 include()
方法。
let list1 = [10, 4, 5];
let list2 = [4, 5];
for (let i = 0; i < list2.length; i++) {
if (list1.includes(list2[i])) {
console.log("Element " + list2[i] + " is in list1.");
}
}
输出将是:
//Element 4 is in list1.
//Element 5 is in list1.
检查代码示例:https://codesandbox.io/embed/sleepy-stitch-jb3c6l?fontsize=14&hidenavigation=1&theme=dark
英文:
The possible solution you have tried is:
const input = document.querySelectorAll('.input');
const data = document.querySelectorAll('.data');
for (let i = 0; i < input.length; i++) {
const inputValue = input[i].value;
if (Array.from(data).some(dateElement => dateElement.innerHTML === inputValue)) {
console.log('Element ' + inputValue + ' is included in datename list.');
// do something if the element is included, e.g.
document.querySelectorAll('.other')[i].classList.add('active2');
} else {
console.log('Element ' + inputValue + ' is not included in datename list.');
}
}
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.
let list1 = [10, 4, 5];
let list2 = [4, 5];
for (let i = 0; i < list2.length; i++) {
if (list1.includes(list2[i])) {
console.log("Element " + list2[i] + " is in list1.");
}
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论