javascript - How to make foreach take effect only when all conditions are met, and jump out of the loop if one does not meet the requirement
PHP中文网
PHP中文网 2017-05-19 10:32:21
0
6
1095


As shown in the picture, I want to automatically select all after single selection. How to achieve this?


Writing like this can be achieved, but every time I change this global variable, I always feel that it is a trap. Is there any good way to solve it?

PHP中文网
PHP中文网

认证0级讲师

reply all(6)
淡淡烟草味

forEach 这个方法是没办法提前退出的。只能用 for Loop

For logic like yours, you can update the view firstmap一下返回符合勾选条件的数组,在forEach

if(this.productList.every(item => item.checked)) {
    _this.checkAllF = true
}
滿天的星座

Use some or every for early exit, and what is the relationship between your function description and early exit

習慣沉默

There is no way,

習慣沉默

@crp205’s answer is correct.
some will interrupt the loop if it returns true, and every returns false Just terminate the loop.some 如果返回 true 就会中断循环, every 有一项返回 false 就终止循环.
数组的遍历只有这两个支持跳出循环了,否则就得用 forOnly these two support for array traversal to break out of the loop, otherwise you have to use for

迷茫

Change forEach to every, then return false

阿神

Why doesn’t js have this syntax:
document.getElementsByxxx.checked(true/false)
means to get the number of checked or unchecked checkboxes/radios in the same category.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template