Error methods for JS traversal array

Source: Internet
Author: User

For (var index in MyArray) {//don ' t actually does this

Console.log (Myarray[index]);
}

Disadvantages:

  • The index value of the array index is the string type "0", "1", "2", and so on, not the number type. When you perform arithmetic operations ("2" +1== "21") may not be the result you expect, so it is inconvenient to convert the type before the operation.
  • The loop body not only iterates through the elements of the array, but even the expando properties. For example, if you have a property called name in your myarray array, the index = = "Name" is also traversed when traversing, so that it executes one more time. Immediately these properties are directly accessible on the prototype chain of the array.
  • The most silent is that, in some cases, this code is arbitrary when iterating over the elements of an array.

Generally use the following two kinds:

Myarray.foreach (function (value) {
Console.log (value);
});

for (var index = 0; index < myarray.length; index++) {
Console.log (Myarray[index]);
}

Error methods for JS traversal array

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.