What is the effect of the operations of filter, some, map, and foreach on an array?

1. Background introduction

Using JavaScript arrays often requires traversing and iterating over the array. And we often use the for statement to iterate over an array. In ES5, five iteration methods have been defined for arrays, namely: filter, some, map, foreach, and every.

2. Knowledge analysis

Five iteration methods, each of which takes two arguments: a function to run on each item and a scope object (optional) to run the function on. The functions passed into these methods receive three parameters: the value of the item in the array, the position of the item in the array, and the array object itself.

1.  filter() is to run the given function on each item in the array and return an array of items for which the function returns true.


2.  map() runs the given function on each item in the array and returns an array of the results of each function call.


3.  forEach() runs the given function for each item in the multiarray, this method has no return value. It just runs the passed-in function on each item in the array, with no return value. Essentially the same as iterating over an array using a for loop.


4.  every() is to run the given function for each item in the array and return true if the function returns true for each item .

5.  some() is to run the given function on each item in the array and return true if the function returns true for any item .


3. Frequently Asked Questions

What is the difference between iteration and traversal, loop, recursion?

4 Solutions

A loop refers to the repeated execution of the same piece of code when conditions are met. For example, the while statement.

Iterate , which refers to visiting each item in a list one by one in some order. For example, the for statement.

Traversal refers to visiting each node in the tree structure according to certain rules, and each node is visited only once.

Recursion is the act of a function calling itself over and over again. For example, programmatically output the famous Fibonacci sequence.

5 coding practice

6. Think outside the box

Two merge methods in ES5

reduce() can implement the functionality of an accumulator, reducing each value of the array (from left to right) to a single value.

reduceRight() has the same function as reduce() , and reduce is traversing the array one by one to the end. And reduceRight() starts from the last item of the array and traverses forward to the first item.

  

 7. References

Iteration and merge method of js array

Difference between loop, iteration, traversal and recursion

8. More discussion

Discussion 1: Will the original array change when iterative operation is performed?

The original array will not change, and a new array will be generated to store the result after the function runs.

Discussion 2: Difference between foreach() and for loop?

foreach is a simplified version of the for loop. Foreach is suitable for the case where the number of loops is unknown, or the calculation of the number of loops is more troublesome. However, some more complex loops still need to use the for loop to be more efficient.

Discussion 3: How to use filter() to remove duplicate elements in an array?


PPT

Tencent Video


Welcome to IT Cultivation Institute
IT Cultivation Institute is a free online IT technology learning platform.
Each occupation provides about 15 tasks for beginners to learn more quickly and efficiently;
all tasks are skill points extracted from real projects,
emphasizing actual combat drills + self-study priority + senior tutors' learning methods
strict daily reporting system, There is a happy atmosphere of communication, discussion and learning, and there are countless brothers and sisters to help you answer your questions.
Dear, are you ready to join us in this perverted growth treasure land?
Click on the official website to start your own journey of self-cultivation!
To join the group, please modify the group name card, the format is as follows:
Region-Occupation-Number
The occupation is the occupation learned in the Xiuzhen Academy. The
number is the number of people in the group when joining the group.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325524832&siteId=291194637