Js array methods: filter(), map(), some(), every(), forEach(), indexOf(), lastIndexOf(),

filter():
Syntax:

var filteredArray = array.filter(callback[, thisObject]);

Parameter Description:

callback: The callback function to execute for each array element.
thisObject : the this object defined when the callback function is executed.

//过滤掉小于 10 的数组元素:

//代码:
function isBigEnough(element, index, array) {
    return element >= 10;
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// 12, 130, 44

Function Description:

Executes the specified function (callback) once for each element in the array, and creates a new array of all the original array elements that return true when the callback function is executed. It only executes the specified function on non-empty elements in the array, elements that have not been assigned or have been deleted will be ignored, and the newly created array will not contain these elements.

The callback function can have three parameters: the current element, the index of the current element and the current array object.

If the parameter thisObject is passed in, it will be used as the this object inside the callback function (callback), if not passed or null, then the global object will be used.

filter will not change the original array, remember: only the elements of the array passed in before the callback function is executed are valid, the elements added after the callback function starts to execute will be ignored, and the callback function starts to execute to the last element. During a period, if an array element is deleted or changed, the time when the callback function accesses the element will prevail, and the deleted element will be ignored.
Note : there must be a return

map():
Syntax:

var new_array = arr.map(function callbackcurrentValue [,index [,array]]){
    //返回new_array的元素
} [,thisArg ])

Parameter Description:

callback: A function that produces a new array element, with three parameters:

  • currentValue: The current element in the array is being processed.
  • index (optional): The index of the element in the array that is being processed.
  • array (optional): The array map is called.

thisArg (optional): The value of this callback to use when executing.

//将所有的数组元素转换为大写:

var strings = ["hello", "Array", "WORLD"];
function makeUpperCase(v)
{
    return v.toUpperCase();
}
var uppers = strings.map(makeUpperCase);
// uppers is now ["HELLO", "ARRAY", "WORLD"]
// strings is unchanged
//结果:["hello", "Array", "WORLD"].map(makeUpperCase) : HELLO, ARRAY, WORLD 

Functional description:
The map() method creates a new array, the result of which is to call a provided function on each element in the calling array. (again skipping the gaps), map does not change the array on which it is called.
map() is a very useful function when dealing with data returned by the server.

some():
Syntax:

arr.somecallback [,thisArg ]

Parameter description: same as above

//检查是否有数组元素大于等于10:

function isBigEnough(element, index, array) {
    return element >= 10;
}
var passed = [2, 5, 8, 1, 4].some(isBigEnough);
// passed is false
passed = [12, 5, 8, 1, 4].some(isBigEnough);
// passed is true

Function description: Execute the specified function (callback) once for each element in the array, until the function returns true, if the element is found, some will return true, if the callback function returns false after each element is executed, some will return false. It only executes the specified function on non-empty elements in the array, elements that are not assigned or have been deleted are ignored.
Can be used to check if a value exists in an array

every():
Syntax:

arr.everycallback [,thisArg ]

Parameter description: same as above

//测试是否所有数组元素都大于等于10:

function isBigEnough(element, index, array) {
    return element >= 10;
}
var passed = [12, 5, 8, 130, 44].every(isBigEnough);
// passed is false
passed = [12, 54, 18, 130, 44].every(isBigEnough);
// passed is true

Function description: Execute the specified function (callback) once for each element in the array, until the function returns false, if the element is found, every will return false, if the callback function returns true after each element is executed, every will return true. It only executes the specified function on non-empty elements in the array, elements that have not been assigned or have been deleted will be ignored

forEach():
Syntax:

arr.forEachcallback [,thisArg ]

Parameter description: same as above

//打印数组内容:

function printElt(element, index, array) {
    document.writeln("[" + index + "] is " + element + "<br />");
}
[2, 5, 9].forEach(printElt);
// Prints:
// [0] is 2
// [1] is 5
// [2] is 9

Function description: The forEach() method executes the provided function once for each array element. There is no return value.

indexOf():
Syntax:

arr.indexOfsearchElement [,fromIndex]

Parameter description:
searchElement: The element to be located in the array.
fromIndex (optional): The index to start the search with. Returns -1 if the index is greater than or equal to the length of the array, which means the array will not be searched. If the provided index value is negative, it is taken as the offset from the end of the array.
Note : If the supplied index is negative, the array is still searched from front to back. If the calculated index is less than 0, then the entire array will be searched. Default: 0 (the entire array is searched).

//查找符合条件的元素:

var array = [2, 5, 9];
var index = array.indexOf(2);// 0

index = array.indexOf(7);  //-1
var arr = [1,2,3,4,5,4,3,2];
var index = arr.indexOf(3);//2
index = arr.indexOf(3,3);//6
index = arr.indexOf(3,-3);//6  -3表示搜索 后面3个 4,3,2

lastIndexOf():
Syntax:

var index = array.lastIndexOf(searchElement[, fromIndex]);

Parameter Description

searchElement: The element to search
fromIndex: The position to start the search, defaults to the length of the array, in this case, all array elements will be searched. The search works in the opposite direction.

Function Description

Compare searchElement and each element of the array to see if they are absolutely consistent (===), and return the index of the current element when an element matches the condition. If not found, just return -1.

var arr = [2, 5, 9, 2];
var index = arr.lastIndexOf(2);//3

index = arr.lastIndexOf(7);//-1

index = arr.lastIndexOf(2, 3);//3 表示从下标为3开始向前找,当然第一次就找到

index = arr.lastIndexOf(2, 2);//0 表示从下标为2开始向前找,就找到第一位

index = arr.lastIndexOf(2, -2);//0 表示从倒数第二位开始向前找,就找到第一位

index = arr.lastIndexOf(2, -1);//3 表示从倒数第一位开始向前找

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325728467&siteId=291194637