Javascript 使用if()语句比较两个数组元素

Javascript 使用if()语句比较两个数组元素,javascript,Javascript,下面的代码工作得很好,但正如您所看到的,所有内容都是手工编写的,如果我想将某些内容推送到该阵列,我必须更改整个代码。 工作示例: for (var s = 0; s < myButtons.length; s++) { if ( (myButtons[s].className == "1" && colorIndex[0] > 1) || (myButtons[s].className == "2" &&

下面的代码工作得很好,但正如您所看到的,所有内容都是手工编写的,如果我想将某些内容推送到该阵列,我必须更改整个代码。
工作示例:

for (var s = 0; s < myButtons.length; s++) {
    if (
        (myButtons[s].className == "1" && colorIndex[0] > 1)
        ||
        (myButtons[s].className == "2" && colorIndex[1] > 1)
        ||
        (myButtons[s].className == "3" && colorIndex[2] > 1)
        ||
        (myButtons[s].className == "4" && colorIndex[3] > 1)
        ||
        (myButtons[s].className == "5" && colorIndex[4] > 1)
        ||
        (myButtons[s].className == "6" && colorIndex[5] > 1)
    ) {continue;}
    alert("things need to be done")
}
for(var s=0;s1)
||
(myButtons[s].className==“2”&&colorIndex[1]>1)
||
(myButtons[s].className==“3”&&colorIndex[2]>1)
||
(myButtons[s].className==“4”&&colorIndex[3]>1)
||
(myButtons[s].className==“5”&&colorIndex[4]>1)
||
(myButtons[s].className==“6”&&colorIndex[5]>1)
){继续;}
警惕(“事情需要做”)
}
我想到了最好的解决方案(不起作用):

for(var s=0;s1)){
继续;
}
警惕(“事情需要做”)
}
}
所以我想要的是:检查数组myButtons.classname==循环变量&&colorIndex[variable from cycle]>1还是相同的元素,但在下一步

第一个算法的工作代码

const colorIndex=[]
颜色索引[0]=201
颜色索引[1]=30002
颜色指数[19]=-25
颜色指数[3]=89
颜色指数[-7]=89
颜色指数[-9]=-26
const myButtons=[…document.queryselectoral(“按钮”)]
对于(var s=0;s1)
||(myButtons[s].className==“2”&&colorIndex[1]>1)
||(myButtons[s].className==“3”&&colorIndex[2]>1)
||(myButtons[s].className==“4”&&colorIndex[3]>1)
||(myButtons[s].className==“5”&&colorIndex[4]>1)
||(myButtons[s].className==“6”&&colorIndex[5]>1)
)
{继续}
警惕(“事情需要做”)
}
console.log(“测试结束”)
1
2.
3.
4.
5.
6.
7.
8.

9
这里是原始代码的一个相当简短且较少硬编码的版本

var myButtons=document.querySelectorAll(“按钮”);
var colorIndex=[0,1,2,0,2,1];
对于(var index=0;index1)继续;
log(“索引“”处的按钮+索引+”'需要修复”);
}
1
2.
3.
4.
5.

6
你只需要制定你想做的事情并用代码表达出来。不要拘泥于具体的索引,试着找出任何可能的索引如何对应于特定的颜色。看看你的例子,你想为每个按钮取类名,减去1,从colorIndex中取颜色,如果颜色小于或等于1,就做些什么

for (let i = 0; i < myButtons.length; i++) {
  // className for current button
  const className = myButtons[i].className
  // parse it into number
  const num = Number(className)
  // if number is valid, take element from colorIndex at number-1 position
  // and see if that's not bigger than 1
  if (num && colorIndex[num - 1] <= 1) {
    console.log('things need to be done for index ' + i)
  }
}
for(设i=0;i如果(num&&colorIndex[num-1]那么…你的问题是什么?如何使代码自动化,当我将在arr color.Index新元素中插入时,现在我必须更改if()结构,我想做代码对我来说是这样的,我猜我在这个问题上做错了什么,我不明白为什么我会被否决???@flemethwow我没有被否决,但我想这是因为这个问题不太清楚你问的是什么,但我认为不值得否决。你展示了你的代码和建议d解决方案,但您不太清楚问题是什么。您可以添加一些内容,例如如何使第一个脚本更通用。提供一个可以重新创建代码的小示例也是一个好主意(在本例中,我们不知道什么是
myButtons
colorIndex
,尽管听起来它们分别是元素和数字的数组)。
for (let i = 0; i < myButtons.length; i++) {
  // className for current button
  const className = myButtons[i].className
  // parse it into number
  const num = Number(className)
  // if number is valid, take element from colorIndex at number-1 position
  // and see if that's not bigger than 1
  if (num && colorIndex[num - 1] <= 1) {
    console.log('things need to be done for index ' + i)
  }
}