Iteration in JS (Array)

What is iteration?It can be simply understood as sequentially accessing each item in the target (array, object, etc.) (in fact, no different from the concept of traversal)

Iterations of arrays are divided by me into two types:

  • lookup
  • ergodic

 

Find:

  1.indexOf(item,start) 

This method searches for the location of the specified element value and returns the subscript.

Parameter: item is the value to find and start is where you want to start (optional parameter).ps:start can take a negative value, raising a chestnut indexOf(x,-5) means that from the sixth to the last (because the first to the last is 0), we always find the tenth in the positive order

If multiple occurrences occur, the first occurrence of the subscript is returned (in fact, the first occurrence is not found later); if not, the -1 is returned.

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

  

2.lastIndexOf(item,start) is the same as the above method except that he is retrograde and starts at the end.Start can also specify a negative value, if it takes -5, it means to start at the place where the subscript is 5 and look forward.

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

 

The 3.find() method returns the value of the first array element that passes the test function.

Parameters: (Item value, Item index, 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 passing the test function.

Parameters: (Item value, Item index, Array itself)

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

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

 

There is nothing to say about the above methods, but the next is the focus.

 

Traversal: There are seven such methods

  1.some()

  2.every()

Both functions accept three values (item value, item index, array itself)

Both methods evaluate each item of the array in turn, returning a boolean type value.The difference is that some s returns true whenever one condition is met, whereas every requires all items to be met before returning true.

Lift a 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 be in the form of an arrow function:

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

Both methods make judgments and do not change the array.

 

  3.forEach()

  4.filter()

  5.map()

Why do these three things come together, because they are like, with some and every one before them, often used as interview questions.

The functions of all three accept three values (item value, item index, array itself)

forEach: It does the same thing as for loops, loops through, and operates on each item.

map: Much like forEach, it returns an array of results from each function call.

filter: Similar to both above, but returns an item whose result of a function call is true (interprets the called function as an expression, that is, returns a new array of items that match the expression)

Differences:

There is no difference between map and foreach in their usage, but!Or a little different, foreach has no return value.

Then, as the name implies, a filter is a filter that filters out items that do not qualify for an array.Therefore, filters can be used very differently and are often used to filter out unwanted array items.

I'd rather lift the chestnuts:

  var 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()

Parameters:

    • Total (initial/previously returned value)
    • Item Value
    • Item Index
    • Array itself

This time I lift the chestnuts directly:

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);

The appellate usage is the most common one used to sum all items of an array.In fact, the soul of this method is to have this prev value, depending on how you can apply it (but in fact, it's not very useful, and it's possible to use an external var sum variable as well).

ps:reduce() and reduceRight() do not perform callback functions on empty arrays.

    

You are foolish and dislike it.

Keywords: Javascript

Added by xpsal on Sun, 01 Sep 2019 19:41:45 +0300