This article describes the method of javascript iteration. Share it for your reference. The specific implementation method is as follows:
Copy the code as follows://filter() Use the specified function to determine whether to return an item contained in the array
var num = [1,2,3,4,5,6,12];
num.filter(function(item, index, array){
return (item > 2); //[3, 4, 5, 6, 12]
});
//map() returns an array where each item in the array is the result of running the incoming parameter on the corresponding item in the original array
var num = [1,2,3,4,5,4,3,2,1];
num.map(function(item, index, array){
return (item * 2); //[2, 4, 6, 8, 10, 8, 6, 4, 2]
});
//every() some() , query whether an item in the array meets a certain condition, every() must pass in each parameter returns true, and the result is true; some() method
//As long as there is one that is true, the result is true
var num = [1,2,3,4,5,4,3,2,1];
num.every(function(item, index, array){
return (item > 2); //false
});
num.some(function(item, index, array){
return (item > 2); //true
})
//forEach() passes in parameters to each item of the array, no return value
var num = [1,2,3,4,5,4,3,2,1];
num.forEach(function(item, index, array){
return item;
})
I hope this article will be helpful to everyone's JavaScript programming.