This article mainly introduces the Array iteration method in javascript. The example analyzes the definition and usage skills of the Array iteration method, for more information about javascript iteration, see the examples in this article. Share it with you for your reference. The specific implementation method is as follows:
The code is as follows:
// Filter () uses the specified function to determine whether to return an included item 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. each item in the array is the result of running the input 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 condition. if every parameter required by every () is returned, true is returned. some () method
// If 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 () is used to input parameters for each array. no return value is returned.
Var num = [1, 2, 3, 4, 5, 4, 3, 2, 1];
Num. forEach (function (item, index, array ){
Return item;
})
I hope this article will help you design javascript programs.