*****************************
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]
- 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.
?