Go →JS array traversal never use for...in ...

Source: Internet
Author: User

See a piece of content is good, but the layout is really bad, forced to die obsessive-compulsive patients ah, directly pull down to find the original link, found, but has disappeared ... 500 error ... For the first time because I really can not see a blog layout, for typesetting and reproduced ...
Reprint Address: http://m.blog.csdn.net/blog/u014357977/38298263

Text ↓

JS Array Traversal I believe everyone is not unfamiliar, many people will think for the For loop and for...in ... Cycle, but never use for...in ... Iterate through the array, especially if you want to write something useful, to be able to migrate the code. Let's take a look at this hateful for...in ....

1. Grammar

 for inch object) {   //do  sth ...}  

There is an object in this syntax, and we know that the concept of objects in JavaScript is very broad, so can objects in this place be arrays? Sure, here's an example:

var a = [1, 2, 3, 4];    for (var in a) {      console.log (a[i]);  }  

This code is not expected, will output 1 2 3 4, a little problem is not, and is very correct usage. But this is usually the right thing to do.

2. For...in ... The problem of traversing an array

First on the code:

    function (value) {// function has been simplified   to        return  right      ;            } var a = [1, 2, 3, 4];        for (var in a) {          console.log (a[i]);      }  

This code is on the top of the code, based on the array to do a bit of expansion. Very simple, just add a function for searching (this function has been simplified to me as above). But let's see what happens when you run:

function (value) {      return right ;  }   

The result of the output, there is a row, this line is a function, not the value we define in the array. That's the problem. Is this really what you meant? The answer is in the negative.

In summary, with for...in ... Normally it does work correctly, but if our code is put in someone else's environment and wants to run, don't use for...in ... to loop the array. And this kind of mistake is often really hidden.

3, how to solve the above problem--honestly write for loop

In the case of the two above (a correct, one error) scenario, the For loop will always run well and the code is as follows:

1     function (value) {  2         return right ;   3     }   4       5     var a = [1, 2, 3, 4];   6      for (var i = 0; i< a.length; i++) {  7        console.log (A[i]);   8     }  

It's best to avoid this mistake by honestly writing a for-loop. Of course there are other ways that this is the way that can be available in the future,

Use For...of ... Statement, but this can only be used in the future, not now,

You can refer to Https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of.

To summarize, the simple way to solve complex problems is the basic idea of our programmers, of course, if you fall into the trap of fancy, you can only climb out of your own.


Go →JS array traversal never use for...in ...

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.