Character array traversal loop

Source: Internet
Author: User

1.for Cycle
for (var i= 0; i<10; i++) {
Console.log (' This content will print 10 times ')
}
for in
    • For-in is used to iterate through the properties of an object

    • The loop is key.

  • For in is not suitable for traversing an array: Because the value of V is not a numeric type but a string type, it can cause great inconvenience in string operations.

  •  
    var myarr = [10,20,40,70];
      for (v in myarr) {
          console. Log (v);      //1
          Span class= "cm-variable" >console. log (typeof v); //string
     }
  ?
?

?
+ For more for iterating over arrays
+ Loop out is the value
?
"' JavaScript
var Myarr = [10,20,40,70];
For (v in Myarr) {
Console.log (v); 10
Console.log (typeof v); Num
}

2.forEach ()
    • Parameter 1 callback

      • Parameter 1: The current element of the loop

      • Parameter 2: Subscript for the current element

      • Parameter 3: The object of the array to which the current element belongs

    • Parameter 2: Optional. The value passed to the function is generally used as the "this" value. If this parameter is empty, "undefined" is passed to the "this" value


VarMyarr= [' 10 ',' 20 ',' 40 ',' 70 '];
VarOtherarr= [' 2 ',' 3 ']
Myarr.ForEach (function ( v,i,) {
        Console. log (v) //value value
        Console. log (i) //corresponding subscript
    console. log (this) //this to the parameters of foreach 2
 },otherarr);
3.map () compatibility problem but it's a dick!!
  • Method creates a new array with the result that each element in the array invokes a supplied function and returns the result.

    • Parameter 1 callback

      • Parameter 1: The current element of the loop

      • Parameter 2: Subscript for the current element

      • Parameter 3: The object of the array to which the current element belongs

    • parameter 2: Optional. The value passed to the function is generally used as the "this" value. if this parameter is null, "undefined" is passed to the "this" value

       
      var myarr = [ ' ", '" ' 70 '];
      console. Log (myarr. Map (x => x * 2)); //[' + ', ' + ', ' + ', ' + ']

ForEach () and map () Traverse

Common

1. All loops iterate through each item in the array.

Each execution of the anonymous function in 2.forEach () and map () supports 3 parameters: the current item in the array item, the index of the current item, and the original array input.

3. This in the anonymous function refers to window.

4. Only the array can be traversed.

4.some ()
    • The Some () method is used to detect whether an element in an array satisfies a specified condition

    • The method executes each element in the array in turn

      • Returns true if an element satisfies the condition, and the remaining element does not perform the detection again

      • Returns False if the condition is not met

        • Empty arrays are not instrumented

        • Does not change the original array


Varage = [4,12,18,20,21];
var judge = age. some (function (v) { //traversal value in age greater than 20 returns ture and returns false
       return v>20;
 })
   console. Log (judge); //true
5.filter ()
    • Grammar


      • Array.filter (function (V,i,arr), Thisvalue)
    • Creates a new array of elements in the new array by examining all the elements in the specified array that meet the criteria.

      • Filter () does not detect an empty array.

      • Filter () Does not change the original array.


VarAge= [4,12,18,20,21,< Span class= "Cm-number" >23,99, ' wo ';
   var judge = age. filter (function (v) {
       return v>< Span class= "Cm-number" >20;
 })
   console. Log (judge); [21,23,99]

Character array traversal 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.