New Features of JavaScript_ECMA5 array, ecmajavascript

Source: Internet
Author: User

New Features of JavaScript_ECMA5 array, ecmajavascript

Var arr = [1, 2, 3, 4, 5, 4, 3, 2, 1];

Method for adding a new location: indexOf lastIndexOf

When the first parameter is set, it indicates that the index position is returned by passing the value (index starts from 0)

Var index = arr. indexOf (4 );
Alert (index); // 3

2. When there are two parameters, the first parameter indicates the start position.

Var index = arr. indexOf (4, 4 );
Alert (index); // 5

3. When they look for Array comparison, '='

LastIndexOf
Var index = arr. lastIndexOf (2 );
Alert (index); // 7

Five new iteration methods

1. every: run a function on each element of the array. If true is returned, true is returned. If one element returns false, false is returned.

var result = arr.every(function(item , index , array){  return item >= 1 ;  });alert(result); //true

2. filter: execute a function for each element of the array to run the given function and return the filtered result.

 var result = arr.filter(function(item , index , array){return item > 2 ;});alert(result); //3,4,5,4,3

3. forEach: loop the value of each item in the array and execute a method

 arr.forEach(function(item, index, array){  alert(item); //1,2,3,4,5,4,3,2,1}); 

4. map can run a function on each element of the array. After the function is executed, the new result is returned.

var result = arr.map(function(item, index, array){  return item*10;});alert(result); //10,20,30,40,50,40,30,20,10   

5. some: run a function on each element of the array. If one item returns true, true is returned. If each element returns false, false is returned.

var result = arr.some(function(item, index, array){  return item >5 ;});alert(result); //false 

Reduce reduceRight

The starting position of the variable is different.

Previous value, current value, index location, array

var result = arr.reduce(function(prev , cur , index , array){  return prev + cur ;});alert(result) //25;var result = arr.reduceRight(function(prev , cur , index , array){  return prev + cur ;});alert(result) //25;

The new features of the above JavaScript_ECMA5 array are all the content shared by Alibaba Cloud. I hope to give you a reference and support for the customer's home.

Related Article

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.