How JavaScript jumps out of the loop, nesting loops.

Source: Internet
Author: User

To implement a function today, each item in array a corresponds to each item in array b, and if it corresponds to the checked of array B, add the Ture property, and if it is not found, add the false attribute to the checked of array B.

If there is any write not welcome master private messages to me, the spirit of sharing the Internet.

Idea One

The idea at first was to add the false attribute if the current B[index] is not present checked=true. But there's a drawback to wasting a lot of loops, so I need to look for a way to jump out of the loop.

 var  a = [1, 2]  var  b = [{id:1}, {id:2}]  for  (var  i = 0; i < b.length; I++) { for  (var  n = 0; n < b.length; n++< Span style= "color: #000000") { if  (A[i] === b[n].id) {b[n].checked  = true  }  else  if  (B[n].checked!==true  ) {b[n].checked  = false  }}}console.log (b)  
Jump out of the loop method

Explanation: X is the equivalent of marking a mark. Break X If the condition is met; (X's naming itself changes) is equivalent to jumping out of this loop, but the outer I loop is unaffected to continue his loop.

var a = [1, 2]var b = [{id:1}, {Id:2}] for (var i = 0; i < B.len Gth i++) {  x:  for (var n = 0; n < b.length; n++) {    if (a[i ] = = = b[n].id      ) {truebreak        x;     Else   {    }  }}console.log (b)

How JavaScript jumps out of the loop, nesting loops.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.