比较列表中的每个项目与另一个项目。

huangapple go评论102阅读模式
英文:

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&lt;=document.querySelectorAll(&#39;.datename&#39;).length;i++){

	if(document.querySelectorAll(&#39;.inputname&#39;)[i].value==document.querySelectorAll(&#39;.datename&#39;)[i].innerHTML){
		console.log(document.querySelectorAll(&#39;.other&#39;)[c].classList)
		   document.querySelectorAll(&#39;.other&#39;)[c].classList.add(&#39;active2&#39;)
	}
}

答案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(&#39;.inputname&#39;), el =&gt; el.value);
for (const el of document.querySelectorAll(&#39;.datename&#39;)) {
	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(&#39;.input&#39;);
const data = document.querySelectorAll(&#39;.data&#39;);

for (let i = 0; i &lt; input.length; i++) {
  const inputValue = input[i].value;
  
  if (Array.from(data).some(dateElement =&gt; dateElement.innerHTML === inputValue)) {
    console.log(&#39;Element &#39; + inputValue + &#39; is included in datename list.&#39;);
    // do something if the element is included, e.g.
    document.querySelectorAll(&#39;.other&#39;)[i].classList.add(&#39;active2&#39;);
  } else {
    console.log(&#39;Element &#39; + inputValue + &#39; is not included in datename list.&#39;);
  }
}

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 &lt; list2.length; i++) {
  if (list1.includes(list2[i])) {
    console.log(&quot;Element &quot; + list2[i] + &quot; is in list1.&quot;);
  }
}

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&amp;hidenavigation=1&amp;theme=dark

huangapple
  • 本文由 发表于 2023年3月10日 00:17:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定