鍍金池/ 問(wèn)答/HTML/ js for 循環(huán)條件成立退出當(dāng)前怎么用?

js for 循環(huán)條件成立退出當(dāng)前怎么用?

var manySelectNote = [1,2,3,4,5,6]
for (i in manySelectNote){
  if (manySelectNote[i]>4) {
      console.log(1)
      break; 
  } else {
      console.log(2)
  }
}

為什么我用 break,return,有時(shí)候都會(huì)執(zhí)行 console.log(2)?

回答
編輯回答
編輯回答
幼梔

會(huì)先打印4個(gè)2

2018年2月10日 04:28
編輯回答
眼雜

按照你的說(shuō)法,這里不應(yīng)該使用循環(huán),可以用every實(shí)現(xiàn)

var manySelectNote = [1,2,3,4,5,6];
if(manySelectNote.every(value=>value<=4)){
    console.log(2);
}else{
    console.log(1);
}
2017年9月8日 16:10
編輯回答
維她命

是先4個(gè)2 然后一個(gè)1 然后退出循環(huán)

2018年9月17日 03:54
編輯回答
款爺

這不是有時(shí)候吧,是4個(gè)2然后1個(gè)1吧?

2017年2月24日 04:56