SoFunction
Updated on 2025-02-28

JavaScript array iteration method

Recently, data processing is often involved in work. Arrays are particularly common. They often need to traverse and convert them. The articles on the Internet are scattered. I have to find the Red Book and read it myself, and note it by the way to facilitate future query.

Common iteration methods for arrays

ECMAScript5 defines 5 iterative methods for arrays. Each method accepts two parameters: the function fn to run on each item and the scope object (optional) that runs the function - affecting the value of `this`.

The function (fn) passed into these methods will receive 3 parameters: item , index , array; such as:

(function(item,index,array){
  //do your staff here;
},this)

Depending on the usage method, the return value after execution of this function may/do not affect the return value in the method.

The functions and return values ​​of these 5 iteration methods are as follows:

ECMAScript5 Array element iteration method

Method name Method Effect Return value
every() Run the specified function for each element in the array Boolean: If each item returns true, return true;
filter() Run the specified function for each element in the array. Array: Returns a new array composed of elements that return true when the function runs.
forEach() Run the specified function for each element in the array null: No return value
map() Run the specified function for each element in the array Array: After the return function is run, a new array composed of new elements is obtained.
some() Run the specified function for each element in the array Boolean: If any function is executed, it returns true.

Simply put:

The every() and some() methods are suitable for conditional judgment of array elements;

filter() , map() methods are suitable for conditional filtering/reprocessing of arrays;

The forEach() method does not operate on the array itself, but only applies secondary applications to array elements;

The following are the methods of using chestnuts:

Let’s first assume a scenario where you get the company’s monthly salary list, assuming your salary is 9,000; the array of salary of company employees is salaries=[8500,12000,9900,9000],

a. Want to know if your salary is the lowest;

b. Want to know if anyone has paid as much as you;

c. Want to know if everyone is treated the same;

d. Want to exchange everyone's salary for data with K as unit

var a,b,c;
var your=9000;
var salaries=[8500,12000,9900,9000];
 a=(function(item,index,array){
  return item<9000
});
(a);//true;Congratulations, there are people who are lower than your salary.b=(function(item,index,array){
  return item== your;
})
(b);//[9000] Haha, someone treats you the samec=(function(item,index,array){
  return item==your; 
});
(c);//false.Not everyone treats you the samed=(function(item,index,array){
  return item/1000 
});
(d);//[8.5,12,9.9,9]

The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!