Iteration (array) JS in

 

Wisdom is iteration? Can be simply understood as each (and in fact, the concept of traversing no difference) access to the target (arrays, objects, etc.) in order

I iterative array is divided into two types:

  • Seek
  • Traversal

 

Find:

  1.indexOf(item,start) 

    The search method specified element value of the position, and returns the next subscript.

    Parameters: item is the value you're looking for, start means you where to start looking for (this parameter is optional). ps: start is a negative value, give chestnut indexOf (x, -5) indicates the start penultimate 6 (as a penultimate thing is 0), have been found positive sequence 0th

    If multiple occurrences of the first occurrence of the subscript is returned (in fact, he would not be found and then later to find); if no-show, returns -1.

    var fruits = ["Apple", "Orange", "Apple", "Mango"];
    var a = fruits.indexOf("Apple");
    console.log(a);//0

  

  2.lastIndexOf (item, start) use the same method as above, different places, he is retrograde, it is looking for from the beginning of the end. start can also specify a negative value, if start taking -5, it means that from the 5 for the location of the start, looking straight ahead.

    var fruits = ["Apple", "Orange", "Apple", "Mango"];
    var a = fruits.lastIndexOf("Apple");
    console.log(a);//2

 

  3.find () method returns the value of the first array element by testing functions.

  :( parameter item value, item index, the array itself)

var numbers = [4, 9, 16, 25, 29];
var first = numbers.find(myFunction);

function myFunction(value, index, array) {
  return value > 18;
}

  

  4. findIndex () method returns the index of the first array element by a test function.

  :( parameter item value, item index, the array itself)

var numbers = [4, 9, 16, 25, 29];
var first = numbers.findIndex(myFunction);

function myFunction(value, index, array) {
  return value > 18;
}

 

Several methods above nothing to talk about, the next is the key.

 

Traversal: There are seven such methods

  1.some()

  2.every()

    Function both accept three values ​​(item value, item index, the array itself)

    Both methods are performed sequentially for each array is determined, the return value of the boolean type. The difference is, some as long as there is a satisfying the condition returns true, and every requires all the conditions are met will return true.

    For chestnut: 

  var a=[1,2,3,4,5,4,3,2,1];
  
var everyResult=a.every(function(item,index,a){   return (item>2);   });   var someResult=a.some(function(item,index,a){   return (item>2);   });
  alert("everyResult:"+everyResult);//false
  alert("someResult:"+someResult);//true

Of course, it can also function as a form of an arrow:

a.some((item)=>{return item>2})

  These two methods only judge, the array will not be changed.

 

  3.forEach()

  4.filter()

  5.map()

  Why these three speak together, as they like, and in front of some, every face questions together often as a test.

  Three functions accept three values ​​(item value, item index, the array itself)

  forEach: and for which it is similar to loop, loop through, for each operation.

  map: forEach and very similar, which returns an array composed of the result of each function call .

  filter: Similar to the above two, but the return is a function of the result of calling an item is true (which will be called understood expression, i.e., in line with this expression returns an array of all new items)

  the difference:

    map and foreach fact, nothing on the use of child difference, but! Still a little difference, foreach is no return value .

    Then, filter the name suggests, the filter is to filter out an array of non-qualifying items. So the purpose, filter will be very different, commonly used to filter out unwanted array of items.

    I still give it chestnuts:

  There are a = [1,2,3,4,5,4,3,2,1 ];
  var filterResult=a.filter(function(item,index,a){
        return (item>2);
    });
var mapResult=a.map(function(item,index,a){ return item*2; });
  var foreachResult=a.forEach(function(item,index,a){
  if(item<5) a[index]=0;
  });
  alert("filterResult:"+filterResult);  //3,4,5,4,3
  alert("mapResult:"+mapResult);     //2,4,6,8,10,8,6,4,2
  alert("foreachResult:"+foreachResult); //underfined
  alert("foreachResult:"+a);        //0,0,0,0,5,0,0,0,0

 

  6.reduce()  

  7.reduceRight()

  parameter: 

    • Total (initial value / values ​​previously returned)
    • Item Values
    • Project Index
    • Array itself

  This time I direct lift chestnuts:

var a=[1,2,3,4,5,4,3,2,1];

var sum=a.reduce(function (prev,cur,index,array){
  return prev+cur;
})
alert("sum:"+sum);

  Appeals usage is the most common usage for an array of all items are summed. In fact Quadrature also possible, with the soul of this method is that the prev value, specifically to see how you apply (though in fact used much, outside of a sum variable var can do)

  ps: reduce () and reduceRight () for an empty array will not be implemented callback function.

    

  Personal humble opinion, not like simmering.

 

Guess you like

Origin www.cnblogs.com/baabaabaa/p/11444281.html