filter ():
Grammar:
var
filteredArray = array.filter(callback[, thisObject]);
Parameter description:
Callback: The callback function to execute for each array element.
Thisobject: The This object that is defined when the callback function is executed.
// filter out the array elements less than 10: // Code: function Isbigenough (element, index, array) { return (element >=);} var = [5, 8, Filtered,].filter (Isbigenough); // A. //
Function Description:
Each element in an array is executed once for the specified function (callback), and a new array is created, which is the original array element whose return value is true when all the callback functions are executed. It executes the specified function only for non-empty elements in the array, no assignment or deleted elements are ignored, and the newly created array does not contain the 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 treated as the this object inside the callback function (callback), and if it is not passed or null, the global object will be used.
Filter does not change the original array, remember: Only the array elements passed in before the callback function executes, the elements that are added after the callback function begins execution are ignored , and the elements of the array are deleted or changed when the callback function begins execution to the last element. When the callback function accesses the element, the deleted element is ignored.
map ():
// converts all array elements to uppercase: 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 //
some ():
Each element in an array executes the specified function once (callback) until this function returns true, and if this element is found, some returns trueif the callback function returns false after each element is executed, some will return false. It executes the specified function only for non-empty elements in the array, no assignment, or the deleted element is ignored.
//Check if there is an array element greater than or equal to ten:functionIsbigenough (element, index, array) {return(Element >= 10);}varpassed = [2, 5, 8, 1, 4].some (Isbigenough);//passed is falsepassed = [12, 5, 8, 1, 4].some (Isbigenough);//passed is true//Results://[2, 5, 8, 1, 4].some (Isbigenough): false//[5, 8, 1, 4].some (Isbigenough): True
every ():
each element in an array is executed once for the specified function (callback) until the function returns false If this element is found, every will return false , if the callback function returns true after each element is executed, every returns true . It executes the specified function only for non-empty elements in the array, no assignment, or the deleted element is ignored
// test if all array elements are greater than or equal to:
function Isbigenough (element, index, Array) { return (element >= 10 var passed = [5, 8,, 44].every (Isbigenough ); // passed is false passed = [N, si, si,, 44].every (Isbigenough); // // result: // [12, 5, 8,, 44].every (Isbigenough) return: false // [12, Si, si, he, 44].every (Isbigenough) returns: true
ForEach ():
//Print Array Contents:functionPrintelt (element, index, array) {Document.writeln ("[" + Index + "] is" + element + "<br/>");} [2, 5, 9].foreach (printelt);//Prints://[0] is 2//[1] is 5//[2] is 9//Results://[0] is 2//[1] is 5//[2] is 9
lastIndexOf ():Grammar
var index = Array.lastindexof (searchelement[, FromIndex]);
Parameter description
Searchelement: The element to search for
FromIndex: The location at which to start the search, which defaults to the length of the array, in which case all the element elements are searched. the search is in the opposite direction.
Function description
Compares the absolute consistency (= = =) of each element of the searchelement and array, and returns the index of the current element when there are elements that meet the criteria. If not found, return-1 directly.
//find the element that meets the criteria:
varArray = [2, 5, 9, 2];varindex = Array.lastindexof (2);//Index is 3index = Array.lastindexof (7);//Index Is-1index = Array.lastindexof (2, 3);//Index is 3index = Array.lastindexof (2, 2);//Index is 0index = Array.lastindexof (2,-2);//Index is 0index = Array.lastindexof (2,-1);//Index is 3//Results://[2, 5, 9, 2].lastindexof (2): 3//[2, 5, 9, 2].lastindexof (7):-1//[2, 5, 9, 2].lastindexof (2, 3): 3//[2, 5, 9, 2].lastindexof (2, 2): 0//[2, 5, 9, 2].lastindexof (2,-2): 0//[2, 5, 9, 2].lastindexof (2,-1): 3
indexOf ():
function as with LastIndexOf (), the search is in the forward direction
// Find the element that meets the criteria:
var array = [2, 5, 9]; var index = array.indexof (2); // index is 0index = array.indexof (7); // Index Is-1 // Results: // //
Js array--filter (), map (), some (), every (), ForEach (), LastIndexOf (), IndexOf ()