How to Implement JavaScript Array Iteration

Source: Internet
Author: User
This article mainly introduces the JavaScript array iteration method, which has good reference value. Next, let's take a look at it. This article mainly introduces the JavaScript array iteration method, which has good reference value. Let's take a look at it with the small Editor.

Data processing is often involved in recent work. arrays are especially common and often need to be traversed and converted. Online articles are scattered, so you have to find out the red books to read them, by the way, it is easy to query later.

Common array Iteration Methods

ECMAScript5 defines five Iteration Methods for arrays. Each method accepts two parameters: the function fn to be run on each item and (optional) The scope object for running the function-the value that affects 'foo.

The function (fn) passed in these methods will receive three parameters: item, index, and array. For example:

array.forEach(function(item,index,array){  //do your staff here;},this)

The returned value after the function is executed may/will not affect the returned value in the method.

The functions and return values of these five iteration methods are as follows:

ECMAScript5 Array element Iteration Method


Method Name Functions Return Value
Every () Run the specified function for each element in the array. Boolean: returns true if each item is true;
Filter () Run the specified function for each element in the array, Array: a new Array composed of elements returned by function execution and true
ForEach () Run the specified function for each element in the array. Null: No Return Value
Map () Run the specified function for each element in the array. Array: returns a new Array composed of new elements after the function is run.
Some () Run the specified function for each element in the array. Boolean: returns true if any function is executed.

To put it simply:

The every () and some () methods are suitable for conditional judgment on array elements;

The filter () and map () methods are suitable for filtering and reprocessing arrays with conditions;

The forEach () method does not operate on the array itself, but only applies the array element twice;

The following describes how to use the following methods:

Let's assume that you have obtained the company's salary list for this month. Assume that your salary is 9000. The employee salary array of the company is salaries = [,].

A. I want to know if your salary is the lowest;

B. Do you want to know if anyone has the same salary as you have;

C. I want to know if all users are treated the same way;

D. I want to change everyone's salary to K.

Var a, B, c; var your = 9000; var salaries = [,]; a = slaries. some (function (item, index, array) {return item <9000}); console. log (a); // true; congratulations, you still have a lower salary. B = salaries. filter (function (item, index, array) {return item = your;}) console. log (B); // [9000] Haha, someone is treated as you c = salaries. every (function (item, index, array) {return item = your;}); console. log (c); // false. not everyone is treated as you do. d = salaries. map (function (item, index, array) {return item/1000}); console. log (d); // [8.5, 12, 9.9, 9]

The above is the details shared by the JavaScript array iteration implementation method. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.