There's a lot of new stuff in ES5, and knowing it helps us write JavaScript, like arrays, we probably don't need to rhythmic for loops.
The write array method is added to the ES5, as follows:
ForEach (JS v1.6)
Map (JS v1.6)
Filter (JS v1.6)
Some (JS v1.6)
Every (JS v1.6)
IndexOf (JS v1.6)
LastIndexOf (JS v1.6)
Reduce (JS v1.8)
Reduceright (JS v1.8)
1, JS commonly used arrays Array object properties:
As shown in the figure, the part marked with a red circle is a new property for ES5.
2, browser support situation:
ie:9+;
Chrome;
firefox2+;
Safari 3+;
Opera 9.5+;
3. Location method
ECMASCRIPT5 defines 2 location methods for an array. IndexOf (), LastIndexOf ();
Both methods receive two parameters: the Xiang (optional) To find indicates the index at which to find the start point.
where the IndexOf () begins the backward lookup from the beginning of the array (position 0), and LastIndexOf () starts looking forward from the end of the array.
Either method returns the position of the item to find in the array, or returns 1 if it is not found;
Example:
var numbers = [1,2,3,4,5,4,3,2,1];
Alert (Numbers.indexof (4)); 4
Alert (Number.lastindexof (4));//5
alert (Number.indexof (4,4));//5
alert (Number.lastindexof (4,4)) ; 3
4. Iterative method
ECMASCRIPT5 defines 5 iteration methods for an array.
4.1, every ()
Definition and Usage: the Every () method is used to detect whether all elements of an array meet the specified criteria (provided by the function).
The Every () method detects all elements in an array using the specified function:
• If an element is detected in an array that is not satisfied, the entire expression returns false and the remaining elements are no longer instrumented.
• Returns true if all elements satisfy the condition.
Note: every () does not detect empty arrays.
Note: every () does not change the original array.
Description: Detects whether all elements of an array ages are greater than 18:
var ages = [M, m, m];
function Checkadult (age) {return age
>=;
}
function MyFunction () {
document.getElementById ("Demo"). InnerHTML = Ages.every (checkadult);
The results are:
False
4.2, some ()
Definition and Usage: the Some () method detects whether an element in an array satisfies a specified condition (provided by the function).
Runs the given function for each item in the array, and returns True if the function returns true for either item;
The code is as follows:
var numbers = [1,2,3,4,5,4,3,2,1];
var someresult = numbers.some (function (item,index,array) {//item refers to algebraic group values; index refers to algebraic group subscript; array refers to the algebraic group itself; return
(item >2);
};
alert (Someresult);
The results are:
True
4.3, filter ()
Definition and Usage: the filter () method creates a new array of elements that check all the elements in the specified array that match the criteria.
Each item in the array runs the given function, returning an array of items that the function returns True.
Description: To return an array of all values greater than 2, the code is as follows:
var numbers = [1,2,3,4,5,4,3,2,1];
var filterresult = numbers.filter (function (item,index,array) {//item refers to algebraic group values; index refers to algebraic group subscript; array refers to the algebraic group itself; return
(item >2);
};
alert (Filterresult);
The results are:
[3,4,5,4,3]
4.4, Map ()
Definition and Usage: the map () method returns a new array in which the elements of the array invoke the value of the function processed by the original array element.
Each item in an array runs the given function, returning an array of the results of each function call.
Description: Multiplies each item in the array by 2, returning the array of these products, the following code:
var numbers = [1,2,3,4,5,4,3,2,1];
var mapresult = Numbers.map (function (item,index,array) {//item refers to algebraic group value; index refers to algebraic group subscript; array refers to algebraic group itself; return
item*2;
});
alert (Mapresult);
The results are:
[2,4,6,8,10,8,6,4,2]
4.5, ForEach ()
Definition and Usage: Each item in an array runs a given function. This method has no return value.
is essentially the same as traversing an array using a For loop. The code is as follows:
var numbers = [1,2,3,4];
Numbers.foreach (function (item,index,array) {
console.log (item);
});
The results are:
1
2
3
4
5, reduce the method
ECMAScript5 adds two methods for narrowing the array: reduce () and reduceright ();
These two methods iterate over all the items of the group, and then build a value that is eventually returned. where the reduce () method begins with the first item of the array, traversing to the last.
Reduceright () begins with the last item in the array and traverses forward to the first item. Both methods receive two parameters: a function called on each item and (optionally) the initial value that is used as the base of the reduction.
Functions passed to reduce () and Reduceright () receive 4 parameters: the previous value, the current value, the index of the item, and the array object.
Description: Use the reduce () method to perform an operation that finds the sum of all the values in an array. The code is as follows:
var values = [1,2,3,4,5];
var sum =values.reduce (function (prev,cur,index,array) {return
prev+cur;
});
alert (sum);
The results are:
15
The above is a small set to introduce the JavaScript ES5 standard new array method of knowledge, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!