New Javascript features: Array Extension

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.