HTML5 Array method

Source: Internet
Author: User

*****************************

foreach method

*****************************

?

<script>
var data = [1,2,3,4];
var sum = 0;

Calculate the value of a data element
Data.foreach (function (_val) {
sum + = _val;
})
Console.log (sum);

The value of each data element plus 1
Data.foreach (function (_val,i,_arr) {
_arr[i] = _val+1;
})
Console.log (data);
</script>

?

*****************************

Map method

*****************************

?

<script>
var a = [1,2,3,4];

Each element in the array *10
b = A.map (function (x) {
return x*10;
})
Console.log (b);
</script>

?

*****************************

Filter method

*****************************

?

<script>
var a = [1,2,3,4];
s = a.filter (function (x) {return x<3});
Console.log (s); Output: [+]
</script>

Usage 2: Compress the vacancy and delete the undefined and null elements

A = A.filter (function (x) {return x!==undefined && x!=null;});

?

*****************************

Every and some methods

*****************************

?

The Every () and some () methods are logical decisions of arrays: they apply the specified function to the array element to determine, returning true or false.

Every a little class with (| | or operation), all values match the judging condition and return true.

A = [1,2,3,4,5]

A.every (function (x) {return x<10;}); Returns true;

Some is a bit of a class (&& and operation), so long as a value conforms to the judging condition, it returns true.

A = [1,2,3,4,5]

    1. Some (function (x) {return x<3;}); returns false;

*****************************

Reduce and reduceright () methods

?

var a = [1,2,3,4,5]

var sum = a.reduce (function (x, y) {return x+y},0); Array summation

var product = a.reduce (function (x, y) {return x*y},1); Array quadrature

Reduce () requires two parameters.

var a = [1,2,3,4,5]

Reduceright () follows the array index from high to low. From 5-4-3-2-1 operations

?

*****************************

IndexOf and LastIndexOf () methods

?

a= [0,1,2,1,0]

A.indexof (1); Output: Find a value equal to 1 from the beginning of the A array. The result is 1, which is a[1]

A.lastindexof (1); Output 3:A[3] is 1.

A.lastindexof (5); Output-1: There is no element with a value of 3.

?

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.