Learn the array iteration method
Every () iterates through each item of the array, returning true if each item meets the criteria, and false if the other way around
Some () iterates through each item of the array, returning true if one of the items meets the criteria, and false if all of the items do not
Map () iterates over each item in the array, which can be given specific conditions and will return to form a new array
Filter () iterates through each item of the array, returning a new array that can be filtered for specific criteria
ForEach () iterates through each item of the array, returning no value
every()
The every() method iterates over each item in the array, returning true if each item meets the criteria and false otherwise.
Situation: There are five children, if the five children are 18 years old or older, you can enter this site
Code examples:
let children = [
{name: ‘Bob’, age: 18},
{name: ‘Peter’, age: 16},
{name: ‘Lynn’, age: 28},
{name: ‘Jack’, age: 38}
]
The // // array requires each item to be true, or false
let isAdults = children.every(child = child.age = 18)
console.log(isAdults); // false because Peter’s age is younger than 18
// Manually change Peter’s age to 20 and test again
children[1].age = 20;
let isAdults2 = children.every(child = child.age = 18)
console.log(isAdults2); // true All children are aged 18 or older, so return true
some()
The some() method returns true for all but one of the items in each iteration, and false for all of the items in each iteration.
Situation: If there are four people, only one of them can pass
Code examples:
let people = [
{name: ‘Bob’, sex: ‘boy’},
{name: ‘Peter’, sex: ‘boy’},
{name: ‘Lynn’, sex: ‘girl’},
{name: ‘Jack’, sex: ‘boy’}
]
// Only one condition in the array is required to return true, otherwise false
let hasGirl = people.some(val = val.sex === ‘girl’);
console.log(hasGirl) // true
map()
The map() method iterates over each item of the array, returning the specified condition to reconstitute the array.
Situation: there are five employees, suddenly the boss of the month to each of the bonus 1W, how much is the salary of each month
Code examples:
let employees = [
{name: ‘Bob’, wage: 5000},
{name: ‘Peter’, wage: 10000},
{name: ‘Lynn’, wage: 15000},
{name: ‘Jack’, wage: 20000}
]
// create a new array for each employee
let employeesWage = employees.map(item = {
return {
name: item.name,
// Add 1000 yuan per person
wage: item.wage + 10000
}
});
console.log(employeesWage);
/ * *
* Returns a new array of employee salaries
* [
* { name: ‘Bob’, wage: 15000 },
* { name: ‘Peter’, wage: 20000 },
* { name: ‘Lynn’, wage: 25000 },
* { name: ‘Jack’, wage: 30000 }
*]
* * /
filter()
The filter() method, for each iteration of the array, filters and returns a new array based on the given criteria
Situation: there are five employees, suddenly the boss of the month to each of the bonus 1W, how much is the salary of each month
Code examples:
let students = [
{name: ‘Bob’, grade: 100},
{name: ‘Peter’, grade: 75},
{name: ‘Lynn’, grade: 80},
{name: ‘Jack’, grade: 95}
]
// Return a new array for students with a score greater than or equal to 90
let awardStudents = students.filter(val = val.grade = 90);
console.log(awardStudents)
/ * *
Output new array:
* [
* { name: ‘Bob’, grade: 100 },
* { name: ‘Jack’, grade: 95 }
*]
* /
forEach()
The forEach() method returns no value forEach iteration of the array
Situation: I want to see the score of each student at work, no return value
Code examples:
// Read everyone’s score, no return value
students.forEach(item = {
Console. log(‘ ${item.name} = ${item.grade} ‘);
/ * *
* output:
* Bob’s grade is 100
* Peter’s grade is 75
* Lynn’s grade is 80
* Jack’s grade is 95
* /
})
// Get the reward student, no return value
awardStudents.forEach(item2 = {
Console. log(‘ ${item2.name} ‘)
/ * output:
* The students who won the prize were Bob
* The students who won the prize are Jack
* /
})
Thinking and summarizing
Often the most basic knowledge can be the most simple and quick to solve the problem, the five iterative methods are very practical in ordinary times, we should remember the advantages of the five array iterative methods, in the need of the situation, can immediately think of which method is the simplest, the most quickly to achieve the demand.