Home  >  Q&A  >  body text

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


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中文网2710 days ago1061

reply all(6)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-19 10:34:21

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

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

    if(this.productList.every(item => item.checked)) {
        _this.checkAllF = true
    }

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-19 10:34:21

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

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-19 10:34:21

    There is no way,

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-19 10:34:21

    @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

    reply
    0
  • 迷茫

    迷茫2017-05-19 10:34:21

    Change forEach to every, then return false

    reply
    0
  • 阿神

    阿神2017-05-19 10:34:21

    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.

    reply
    0
  • Cancelreply