- Iterating functions: Operate to return values by applying each element in an array.
- Does not return a new array
- arr.forEach(callback): Executes the function once for each item in the array, in ascending order
callback
. - arr.every(callback): tests whether all elements in an array pass the test of a specified function. It returns a Boolean value.
true
. The result of each implementation istrue
或arr
Is empty.false
. Element exists, and the result isfalse
.
- arr.some(callback): tests whether at least 1 element in the array passes the provided function test. It returns a Boolean value.
true
. There is an execution fortrue
To return totrue
.false
. Each implementation isfalse
或arr
Is empty.
- arr.reduce(callback): Executes a function on each element in the array
callback
(ascending execution) aggregates its results into a single return value.
- arr.forEach(callback): Executes the function once for each item in the array, in ascending order
/** * arr. ForEach (callback): executes the function callback */ forEach item in the array, in ascending order
var array = [ 'a'.'b'.'c' ];
array.forEach(element= > console.log(element));
/** * std a b c */
function square(num) {
console.log(num, num * num);
}
var num = [ 1.2.3 ];
num.forEach(square);
console.log(num);
/** * std 1 1 2 4 3 9 */
Copy the code
/** * arr.every(callback): Tests whether all elements in an array pass the test of a given function. It returns a Boolean value. * Note: This method returns true in any case if an empty array is received. */
function isEven(num){
return num % 2= = =0;
}
var num = [ 2.4.6.8.10 ];
var even = num.every(isEven);
if(even){
console.log("All the numbers are even.");
}else{
console.log("Not all numbers are even.");
}
/** * STD all numbers are even */
Copy the code
/** * arr.some(callback): tests whether at least one element in the array passes the provided function test. It returns a Boolean value. * Note: This method returns false. */ in any case if an empty array is received
function isEven(num){
return num % 2= = =0;
}
var num = [ 1.2.3.4.5.6.7.8 ];
var someEven = num.some(isEven);
if(someEven){
console.log("Some numbers are even.");
}else{
console.log("No number is even.");
}
/** * STD Some numbers are even */
Copy the code
/** * reduce(): Performs the function callback (ascending execution) on each element in the array to summarize its result into a single return value. */
function add(num1, num2){
return num1 + num2;
}
var num = [ 1.2.3.4 ];
var sum = num.reduce(add);
console.log(sum);
function concat(str, i) {
return str + i;
}
var words = [ "I am "."a "."coder " ];
var re = words.reduce(concat);
console.log(re);
/** * std 10 I am a coder */
Copy the code
- Returns the value of the new array
- Arr.map (callback): Creates a new array, the result of which is the return value of each element in the array after calling the supplied function once.
- Arr.filter (calllback): Creates a new array containing all the elements that pass the function test.
/** * arr.map(callback): Creates a new array, the result of which is the return value of each element in the array after calling the provided function once. */
function addFive(grade){
return grade += 5;
}
var grade = [ 77.82.88.95.90 ];
var result = grade.map(addFive);
console.log(result);
/** * std [ 82, 87, 93, 100, 95 ] */
var array = [1.4.9];
var map = array.map(x= > x * 2);
console.log(map);
/** * std [ 2, 8, 18 ] */
Copy the code
/** * arr.filter(callback): Creates a new array containing all elements that pass the function test. */
var words = [ "spray"."limit"."elite"."exuberant"."destruction"."present" ];
var result = words.filter(word= > word.length > 6);
console.log(result);
/** * std [ 'exuberant', 'destruction', 'present' ] */
function passing(num){
return num >= 60;
}
var grades = [];
for(var i = 0; i <=10; i ++){ grades[i] =Math.floor(Math.random() * 101);
}
var pass = grades.filter(passing);
console.log('The scores of the 10 randomly generated students are:' + grades);
console.log('The passing marks are:' + pass);
/** * 10 students randomly generated by STD are: 30, 25, 77, 12, 74, 34, 41, 61, 57, 49, 92 Passing scores are: 77, 74, 61, 92 */
Copy the code
Ideas, procedures for reference
-
wangzheng0822: Array
-
MDN
-
Note: Program output compiled by WebStorm