How to create a JavaScript For Each Loop

This question already has an answer here:

  • For-each over an array in JavaScript? 23 answers

  • The for in is used to iterate over properties on the object. It is not the same as a regular foreach. Use a for loop for this


    var myArray = ["Bob", "Nancy", "Jessie", "Frank"];
    var arrayLength = myStringArray.length;
    
    for (var i = 0; i < arrayLength; i++) {
        //Do something with element myArray[i]
    }
    

    I guess you need something like this.

    Edit: Your array has only 4 elements. In the 2nd line I save the length of your array (4 elements --> length is 4) in the variable 'arrayLength'. Then I wrote a simple for-loop which cycles the 'i' from 0 till 3 so you can access your elements from your array as 'myArray[i]'.

    链接地址: http://www.djcxy.com/p/12064.html

    上一篇: javascript中var和var之间的区别

    下一篇: 如何为每个循环创建一个JavaScript