js basis of an array of iterative methods

Array.forEach()

forEach () method is called once for each array element function (callback function).

var txt = "";
var numbers = [45, 4, 9, 16, 25];
numbers.forEach(myFunction);

function myFunction(value) {
  txt = txt + value + "<br>"; 
}
Array.map()

map () method to create an array of new functions for the implementation of each array element by.
map () method does not perform the function of the array element values.
map () method does not change the original array.

This example will each array value is multiplied by 2:

var numbers1 = [45, 4, 9, 16, 25];
var numbers2 = numbers1.map(myFunction);

function myFunction(value, index, array) {
  return value * 2;  
}
console.log(numbers2 );     //90,8,18,32,50

Note that the function has three parameters:
item value of
the item index
array itself
when only the callback parameter value may be omitted and the array index parameters:

var numbers1 = [45, 4, 9, 16, 25];
var numbers2 = numbers1.map(myFunction);

function myFunction(value) {
  return value * 2;
}
Array.filter()

filter () method creates a new array by the array elements tested included.
Examples of this element 18 with a value greater than create a new array:

var numbers = [45, 4, 9, 16, 25];
var over18 = numbers.filter(myFunction);

function myFunction(value, index, array) {
  return value > 18;
}
console.log(over18);     //45,25

Note that this function takes three parameters: the
item value of
the item index
array itself
in the above example, the callback function does not use the index and array parameters, they can be omitted:

var numbers = [45, 4, 9, 16, 25];
var over18 = numbers.filter(myFunction);

function myFunction(value) {
  return value > 18;
}
Array.reduce()/Array.reduceRight()

reduce () / reduceRight () method runs on each array element function to generate (which decrease) a single value.
reduce () / reduceRight () method does not reduce the original array.

This example determined the sum of all the numbers in the array:

//reduce() 方法能够接受一个初始值:
var numbers1 = [45, 4, 9, 16, 25];
var sum = numbers1.reduce(myFunction, 100);

function myFunction(total, value) {
  return total + value;
}
//sum=199
var numbers1 = [45, 4, 9, 16, 25];
var sum = numbers1.reduceRight(myFunction);

function myFunction(total, value) {
  return total + value;
}
//sum=99
Array.every()

every () method checks whether the values ​​of all array by testing.

This example checks all array value is greater than 18:

var numbers = [45, 4, 9, 16, 25];
var allOver18 = numbers.every(myFunction);

function myFunction(value) {
  return value > 18;
}
//返回false
Array.some()

some () method checks whether a certain array value passed the test.

Examples of some of the arrays of this check value is greater than 18:

var numbers = [45, 4, 9, 16, 25];
var someOver18 = numbers.some(myFunction);

function myFunction(value, index, array) {
  return value > 18;
}
//返回true
//请注意此函数接受 3 个参数:项目值,项目索引,数组本身
Array.indexOf()

the indexOf () method searches in the array element values ​​and returns its position.

NOTE: The location of the first item is 0, the position of the second item is 1, and so on.

//检索数组中的项目 "Apple":
var fruits = ["Apple", "Orange", "Apple", "Mango"];
var a = fruits.indexOf("Apple");
//Apple 被找到的位置是:1

If the item is not found, Array.indexOf () returns -1.
If the project appears more than once, the position of the first occurrence is returned.

Array.lastIndexOf()

Array.lastIndexOf () and Array.indexOf () is similar, but starting from the end of the array search.

//检索数组中的项目 "Apple":
var fruits = ["Apple", "Orange", "Apple", "Mango"];
var a = fruits.lastIndexOf("Apple");
Array.find()

find () method returns the value of the first array element by testing functions.

This example lookup (return) of the first element is greater than 18:

var numbers = [4, 9, 16, 25, 29];
var first = numbers.find(myFunction);

function myFunction(value, index, array) {
  return value > 18;
}
//大于 18 的第一个值是:25
//3 个参数:项目值,项目索引,数组本身
Array.findIndex()

findIndex () method returns the index of the first element array by a function test.

This example Finds the index of the first element is greater than 18:

var numbers = [4, 9, 16, 25, 29];
var first = numbers.findIndex(myFunction);

function myFunction(value, index, array) {
  return value > 18;
}
//大于 18 的第一个值的索引是:3

Guess you like

Origin www.cnblogs.com/jessie-xian/p/11595926.html