Home >Web Front-end >JS Tutorial >About js iterator method
An iterator is an object that can be used to traverse some or all elements in a standard template library container. Each iterator object represents a certain address in the container. There are two types of iterator methods for js arrays: one does not generate a new array, and the other generates a new array.
The first method: does not produce any new array, instead, they either perform some operation on each element in the array, or return a value.
1. forEach() method, which receives a function as a parameter and uses the function for each element in the array.
<script> function arr(num){console.log(num+1)}; var array = [1,2,3,4,5,6]; array.forEach(arr); </script>
2.every() and some(), both methods accept a function whose return value is a Boolean type, and use this function for each element in the array. The every() method returns true if the function returns true for all elements. The some() method returns true as long as one element uses this function to return true.
<script> var arr = [1,3,4,5,6,7,8]; function even(num){return num%2 ==0}; var sum = arr.every(even); var nums = arr.some(even); console.log(sum)//false; console.log(nums)//true;
</script>
3. The reduce() method accepts a function and returns a value. This method will start with an accumulated value, continue to call the function on the accumulated value and subsequent elements in the array, until the last element in the array, and finally return the accumulated value. You can also concatenate the string array elements into a long string
function add(total,num){return total+-*/num}; var arr =[1,2,3,4]; var sum = arr.reduce(add);
js also provides the reduceRight() method, the difference is that it is in the opposite order to the reduce() method , from right to left.
Second method Iterator method to generate new array
There are two iterator methods that can generate new arrays: map() and filter(). map() is a bit like forEach(), which uses a function on each element in the array. The difference between the two is that map() returns a new array, which is the result of applying a function to the original elements.
function add(num) { return num+=5; } var words =[1,2,3,4,5]; var sum =words.map(add); console.log(sum);//[6,7,8,9,10]
filter() is similar to every(), passing in a function whose return value is a Boolean type. The difference from every() is that when all the elements in the array are When the function is applied to elements and the results are all true, this method does not return true, but returns a new array containing the elements whose results are true after applying the function.
function add(num) { return num%2 == 0; } var words =[1,2,3,4,5]; var sum =words.filter(add); console.log(sum);//[2,4];
<br/>
filter() can also be used to filter string arrays. For example:
function add(str){if(str).indexOf("cie")>-1){return true;}return false;} var words =["recieve","deceit","deceive"] var miss = words.filter(add); console.log(miss)//["recieve"]
The above content is about the js iterator method, I hope it can help everyone.
Related recommendations:
How to use php iterator to implement a Fibonacci sequence function example analysis
How to implement JavaScript iterator pattern and detailed explanation of usage examples
The above is the detailed content of About js iterator method. For more information, please follow other related articles on the PHP Chinese website!