ForEach () map () some () [array expansion method]

Source: Internet
Author: User

2.7.1 Arr.foreach ()

Parameters and Meanings

var arr = [' A ', ' B ', ' C ', ' d ', ' e '];

var RT = Arr.foreach (function (A, B, c) {
console. Log (' Three parameters ', a, b, c);
} ) ;

console. log (' return value ', RT);

The first 1 parameters are each element in the array

The first 2 parameters are indexed

The first 3 parameters are the original array

Characteristics

The 1> ForEach method executes the callback function once for each item in the array that has a valid value in ascending order , those that have been deleted (using the Delete method, and so on) or uninitialized items that are skipped (but do not include those values undefined items)

In the 1> callback function, whatever you return , the end result is undefined .

2.7.2 Arr.map ()

Parameters and Meanings

var arr = [' A ', ' B ', ' C ', ' d ', ' e '];

var RT = Arr.map (function (A, B, c) {
console. Log (' Three parameters ', a, b, c);

} ) ;

console. log (' return value ', RT);

The first 1 parameters are each element in the array

The first 2 parameters are indexed

the 3 parameter is an array of return values for each call to the callback function

Characteristics

The map method invokes the callback function sequentially for each element in the original array . callback The return value (including undefined) after each execution is combined to form a new array. the callback function is only called on the indexed index, and those indexes that have never been assigned a value or deleted by using delete are not called.

Case

Add 2 to each item in the array

var arr = [' A ', ' B ', ' C ', ' d ', ' e '];

var RT = Arr.map (function (item, index, arr) {
console. Log (' Three parameters ', item, index, ARR);
return item + 2;
} ) ;

console. log (' return value ', RT);

2.7.3 Arr.some ()

Parameters and Meanings

var arr = [1, 2, 3, 4, 5];

var RT = Arr.some (function (item, index, arr) {
console. Log (' Three parameters ', item, index, ARR);
return item > 9;
} ) ;

console. log (' return value ', RT);

judging each item in the array, if there is one that satisfies the item > 9 then the some method returns true, otherwise false

Characteristics

Some   callback  callback " " Span style= "font-family: Equal line" > (can be converted to a Boolean true some    true some  return  false callback  has a value " is called on the index , it is not invoked on indexes that have been deleted or have never been assigned a value.

var arr = [1, 2, 3, 4, 5];

var RT = Arr.some (function (item, index, arr) {
console. Log (' Three parameters ', item, index, ARR);
return item > 3;
} ) ;

console. log (' return value ', RT);

ForEach () map () some () [array expansion method]

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.