In ecmascript5, some new methods are introduced for array objects, which are listed below:
Item positioning:Indexof ()/Lastindexof ():
<script type="text/javascript">var arr=[1,2,3,4,5,6,5,4,3,2,1];console.log(arr.indexOf(2));console.log(arr.lastIndexOf(2));</script>
Both methods accept two parameters: the item to be searched and the optional index from where to start searching.
Iteration Method:
Every ()-------- Run the given function on each element of the array. If the given function returns true for each item, true is returned.
Filter ()--------- Run the given function on each element of the array and return the elements that contain the true value returned by the given function.
Foreach ()----- Run the given function on each element of the array. This method has no return value.
Map ()--------- Run the given function on each element of the array and return an array consisting of the return values of each given function
Some ()-------- Run the given function on each element of the array. If the given function returns true on any element, true is returned.
These methods both accept two parameters: a function running on each project and an optional scope object for where to run the function. The function accepts three parameters: the array project, the position of the array project in the array, and the array object itself.
<script type="text/javascript">var arr=[1,2,3,4,5,6,5,4,3,2,1];var result1=arr.every(function(item,index,array){return item>2});var result2=arr.filter(function(item,index,array){return item>2});var result3=arr.map(function(item,index,array){return item>2});var result4=arr.some(function(item,index,array){return item>2});arr.forEach(function(item,index,array){console.log(item+=2);});console.log(result1);console.log(result2);console.log(result3);console.log(result4);console.log(arr);</script>
Of these methods, the two most similar are every () and some (). They both query whether a condition is matched. For every (), the input function must return true for each parameter to return true. Otherwise, the method returns false. On the contrary, some () returns true if even one element returns true for the input parameter.
Foreach () does not return values. It is basically the same as an array for loop iteration.