The example in this article describes the method of javascript iteration. Share it with everyone for your reference. The specific implementation method is as follows:
num.filter(function(item, index, array){
return (item > 2); //[3, 4, 5, 6, 12]
});
//map() returns an array, each item in the array is the result of running the passed parameters 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 every parameter to return true, and the result will be true; some() method
//As long as one of them 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 parameters to each item in the array and has 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 design.