For Loop vs. in loop

Source: Internet
Author: User

JSON is a data format in JS.
var obj={a:15,b:8,c:12} JSON Array object
var arr=[15,8,12]; Array
alert (OBJ.A); ---15
Alert (obj[' a ']);---15
Alert (arr[0]);---15


for (Var i=0;i<arr.length;i++) {//array loop first notation
Alert (i+ "=" +arr[i]);
} popup: 0=15 1=8 2=12

for (var i in arr) {//array loop second notation
}

for (var i in obj) {//json loop
Alert (i+ "=" +obj[i]);
} popup: A=15 b=8 c=12

Summary: Arrays can be used for loops or for in loops (the difference: The For loop can set the start number of I, can start a loop from any position, but the for in loop cannot be set, only from the first to the last).
JSON can only be used for the in loop because the JSON subscript is an irregular string with no length.
Therefore, the general array is used for loops, and JSON is used for the in loop.

For Loop vs. in loop

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.