Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何防止在JS中使用多个else if语句?_Javascript_Html_Dictionary_If Statement - Fatal编程技术网

Javascript 如何防止在JS中使用多个else if语句?

Javascript 如何防止在JS中使用多个else if语句?,javascript,html,dictionary,if-statement,Javascript,Html,Dictionary,If Statement,我有一个脚本,它在我的页面中穿行,并寻找任何工具提示来填充正确的成分。 但我想知道是否有一种方法可以像这样优化它: var indiv = document.getElementById("ingredients"); var s = indiv.getElementsByTagName("SPAN"); var i; for (i = 0; i < s.length; i++) { var ss = s[i].parentEleme

我有一个脚本,它在我的页面中穿行,并寻找任何工具提示来填充正确的成分。 但我想知道是否有一种方法可以像这样优化它:

var indiv = document.getElementById("ingredients");
var s = indiv.getElementsByTagName("SPAN"); 
var i;
    
for (i = 0; i < s.length; i++) {
  var ss = s[i].parentElement.textContent;
      
  var ingredient1 = ss.includes("test1");
  var ingredient2 = ss.includes("test2");
 
  if (ingredient1) {
    s[i].textContent = "this is the number 1 ingredient";
  } else if (ingredient2) {
    s[i].textContent = "this is the number 2 ingredien";
  } else {
    s[i].textContent = "unknown ingredient!";
  }
}
function getIngredientNr(ingredientStrList, text){
  const nr = ingredientStrList.findIndex(e=>text.includes(e))+1;
  return nr || undefined; //returns undefined if no ingredient str matched
}
因此,脚本的目的是将
中的未知值更改为正确的值。

const-components=[Ingredit1、Ingredit2、Ingredit3、Ingredit4];
设指数=0;
让未知=错误;
用于(成分的常量成分){
if(component){console.log(`this is component number${index+1}`);unknown=true;break;}
索引++;
}
if(未知)console.log(“未知”);

用于循环和中断。

换句话说:一组表示特定成分的字符串。 如果这些字符串以与其所谓的
成分nr
相关的顺序存储在一个简单数组中,那么在识别名为
ss
的字符串变量的值中包含哪些成分字符串后,只需使用
索引+1

首先,让我们创建前面提到的数组:

const ingredientStrList = ['test1', 'test2', 'test3', 'test4'];
现在,让我们创建一个虚拟
ss
变量,该变量具有一个可能的可测试值:

const ss = '12test12';
现在我们需要一种方法来迭代数组中的所有字符串,并检查它们是否包含在字符串
ss
中,如果是,则返回匹配的
成分字符串的所需
索引

为此,我们可以使用which为我们进行迭代,如果找到匹配项,还可以返回索引。但是我们必须告诉它应该使用哪个函数来检查成分字符串

当然我们使用的<代码>ss.包括<代码>为我们使用的<代码>当然我们使用的<代码>包括包括在内的<代码>为我们使用的<代码>当然我们使用的<代码>当然我们使用的<代码>ss.包括包括<代码>为我们所需的检查功能,我们使用的<代码>为我们所需的检查功能,b,b,b,u,t,h,e,r,r,r,r,r,e,r,e,e,e,r,e,e,e,e,e,e,包括包括包括包括包括包括包括包括在内在内在内在内在内的,包括包括包括<代码>代码>为我们,为为那所需的,包括包括包括包括包括在内,为为那所需的代码>代码>我们,为为那所需的那所需的那所需的必要的检查功能,包括包括包括包括包括包括包括包括包括包括,代码>代码>代码>代码>为我们所需的,包括包括包括包括包括包括包括包括包括,代码>为3.2,l,l,l,l,l,l,b,a,a,b,a,a,b,a,b,a,b,a,a,a,a,a,a,b,b,b,a,b,a,a,a,a,a,b,b,a,a,a,a,a,b,b,b,b,b,b,b,b,b,b,a,b,a,a,a,b,a,a,a,a,b,a,a,a,b,b,b,a,a,b,b,a,a,a,a,b,a,a,b,b,b,b,a,a,b,a,a,a,a,a,b,b,a,b,a,a,b,b,b,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a[̶2̶]k̶e̶y̶w̶o̶r̶d̶i̶n̶t̶e̶r̶n̶a̶l̶y̶ 8.0奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈4.0 0 0,f,f,0 0,0 0,0 0,0 0,0,0 0,0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 828222;,[[[[[[[[3]奈奈奈奈奈奈奈奈奈奈奈蒂蒂蒂,,,[[[[[3]]][[3]]][[3]]]][[[3 3 3 3 3][[3,,,,,[[[[[[[[3]]]]]]]]]]]]][[[[[[3,,,,[[[[[[[[[3]]]]]]]]奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈在本研究中,c„c,c̶c,c,c̶c,c,c,c,c,c,c,c,c,c,c,c,c,c̶c,c,c,c,c,c,c,c,c,c,c,c,a,c,a,c,c,a,a,c,a,a,a,c,a,a,a,a,c,a,a,a,a,a,a,a,e,u,S,e,t,h,e̶[̶b̶i̶n̶d̶][̶3̶]-[f̶,u̶,n̶,c̶t̶i̶,o̶,n̶

因此,通过使用前面提到的所有内容,我们可以得出如下函数:

var indiv = document.getElementById("ingredients");
var s = indiv.getElementsByTagName("SPAN"); 
var i;
    
for (i = 0; i < s.length; i++) {
  var ss = s[i].parentElement.textContent;
      
  var ingredient1 = ss.includes("test1");
  var ingredient2 = ss.includes("test2");
 
  if (ingredient1) {
    s[i].textContent = "this is the number 1 ingredient";
  } else if (ingredient2) {
    s[i].textContent = "this is the number 2 ingredien";
  } else {
    s[i].textContent = "unknown ingredient!";
  }
}
function getIngredientNr(ingredientStrList, text){
  const nr = ingredientStrList.findIndex(e=>text.includes(e))+1;
  return nr || undefined; //returns undefined if no ingredient str matched
}
下面是一个测试,您可以运行该测试来说服自己它按预期工作:

函数getIngredientNr(ingredientStrList,text){
const nr=ingredientStrList.findIndex(e=>text.includes(e))+1;
返回编号| |未定义;
}
常量成分=[
“柑橘柠檬果水”,
“弗吉尼亚金缕梅水”,
“甘油”
];
document.querySelectorAll(“#配料>div>span”).forEach(s=>{
const ss=s.parentNode.innerText;
const ingNr=GetingCreditnr(成分,ss);
s、 innerText=ingNr
?`这是${ingNr}成分的数字`
:“未知成分!”;
});

柑橘柠檬水,
未知的
弗吉尼亚金缕梅水,
未知的
甘油
未知的

你也可以发布你的html片段吗?
我知道Python中有一个用词,但我找不到一个好方法在JS中实现这一点。
JS有对象。优化它意味着什么?更好的性能、更好的可读性还是其他什么?@ibrahimmahrir是的,我用我的html的一部分编辑了这篇文章。你为什么渲染emp然后在js中更新工具提示?为什么不将它们与html一起呈现?如果它是未知的成分呢?这种方法没有考虑到原始方法在循环时,实际上在每次迭代中,通过
ss.includes(“test1”)测试两种特定成分的存在性
ss.includes(“test2”)
。在开始向OP抛出代码之前,必须弄清楚此测试是否仍然是OP故事的重要部分。非常感谢您的代码工作,但这并不完全是我的目的。很抱歉,这是我的错,我没有解释清楚。这与成分的数量无关,更重要的是它的“翻译”如:柑橘柠檬水果水->这是一些柠檬水,金缕梅维吉尼亚水->它是另一种东西,甘油->又是另一种东西。那么它将与2常量一起工作?有没有办法“链接”2数组?第一个数组将包含如下成分:常量成分=[‘柑橘柠檬果水’、‘金缕梅弗吉尼亚水’、‘甘油’];第二个包含第一个的“翻译”,如:const配料=[‘这是纯柠檬’、‘这是金缕梅花提取物’、‘这是一些甘油’];当它在html中找到数组中包含的值时,它会在第二个数组中显示等价的值(将放置在同一索引中)?提前感谢