!Array.includes()=不包含(在js中)? [英] !Array.includes() = does not include (in js)?

查看:1414
本文介绍了!Array.includes()=不包含(在js中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用

 if(!array.includes()){

,以检查用户输入的字对照数组中随机选择的单词以查看两个共有多少个字母,并将数组中没有这些字母的所有单词切成薄片。但是!array.includes()似乎没有像我预期的那样 - 它正在切掉数组中的所有值。



我有什么试过:



这是相关代码的样子:



 comWords = inputWord + compWord; 
comWords = comWords.split('')。sort()。join('');
match = comWords.length - comWords.replace(/(\ w)\1 + / g,'$ 1')。length;
for(let e = 0; e< 5; e ++){
for(let f = 0; f< possWords.length; f ++){
if(match ===) 0){
if(possWords [f] .includes(compWord [e])){
possWords.splice(f,1);
}
} else {
if(!possWords [f] .includes(compWord [e])){
possWords.splice(f,1);
}
}
}
}





第一个包含()声明似乎工作正常,但我似乎弄乱了!includes()一个。这是你如何检查是否包含某些内容或者我是否在完全错误的轨道上?

解决方案

1')。length;
for(let e = 0; e< 5; e ++){
for(let f = 0; f< possWords.length; f ++){
if(match ===) 0){
if(possWords [f] .includes(compWord [e])){
possWords.splice(f,1);
}
} else {
if(!possWords [f] .includes(compWord [e])){
possWords.splice(f,1);
}
}
}
}





第一个包含()声明似乎工作正常,但我似乎弄乱了!includes()一个。这是你如何检查某些内容是否包含在内还是我在完全错误的轨道上?


你是否尝试将整个表达式放在支架之间以用于否定部分?

类似于:

  if (!(possWords [f] .includes(compWord [e ])))



我没有做太多的javascript,但如果我要面对你的问题,这就是我会尝试的。

麻烦。


I'm attempting to use

if (!array.includes()) { 

in order to check a user inputted word against a randomly chosen word in an array to see how many letters the two have in common and slice out all words in the array that don't have those letters. But !array.includes() does not seem to be behaving as I expected -- it's slicing out all the values from the array.

What I have tried:

This is what the relevant code looks like:

comWords = inputWord + compWord; 
comWords = comWords.split('').sort().join(''); 
match = comWords.length - comWords.replace(/(\w)\1+/g, '$1').length; 
for (let e = 0; e < 5; e++) {
	for (let f = 0; f < possWords.length; f++) {
		if (match === 0) {
			if (possWords[f].includes(compWord[e])) {
				possWords.splice(f, 1); 
			}
		} else {
			if (!possWords[f].includes(compWord[e])) {
				possWords.splice(f, 1); 
			} 
		} 
	}
}



The first includes() statement seems to be working fine, but I seem to be messing up the !includes() one. Is this how you check if something does not include or am I on the complete wrong track here?

解决方案

1').length; for (let e = 0; e < 5; e++) { for (let f = 0; f < possWords.length; f++) { if (match === 0) { if (possWords[f].includes(compWord[e])) { possWords.splice(f, 1); } } else { if (!possWords[f].includes(compWord[e])) { possWords.splice(f, 1); } } } }



The first includes() statement seems to be working fine, but I seem to be messing up the !includes() one. Is this how you check if something does not include or am I on the complete wrong track here?


Have you tried putting the whole expression between brackets for the negated part?
Something like:

if (!(possWords[f].includes(compWord[e])))


I don't do much javascript, but this is what I would try if I would be facing your issue.
Kindly.


这篇关于!Array.includes()=不包含(在js中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆