The newly added API interface forEach map filter in Array in ES5 some every indexOf lastIndexOf reduce reduceRight

Source: Internet
Author: User

The newly added API interface forEach map filter in Array in ES5 some every indexOf lastIndexOf reduce reduceRight

 
var array = [23,48,66,2];

ForEach: loop, traversing array; no return value. Similar to for Loop
array.forEach(function( value, index, array ) {  console.log(value);  return value *  value;} );

Map: ing meaning, ing returns a new array with a returned value; filterArr: returns a new object
var mapArr = array.map(function(value, index, array ){ return value *  value;});

Filter: the meaning of filtering; return value; filterArr: return the new array after filtering
var filterArr = array.filter(function(value, index, array ) {  if ( value > 20 ) {    return true;  }  return false;});

EveryArr: Meaning of each and every item; return value; every: Return boolean true or false;
var everyArr = array.every(function(value, index, array){  if ( value > 1 ) {    return true;  }});

IndexOf: the first is the retrieved value, and the second is the position to be retrieved starting from 0.
console.log(array.indexOf(23,0));

LastIndexOf: the first is the value to be retrieved, and the second is the position to be retrieved starting from arr. length-1.
console.log(array.lastIndexOf(2,10));

// Initial settings Previous = initialValue = 1, current = 2
// The first iterationPrevious = (1 + 2) = 3, current = 3 
// The second iterationPrevious = (3 + 3) = 6, current = 4 
// The third iterationPrevious = (6 + 4) = 10, current = undefined (Exit) 
var reduce = [1,2,3,4].reduce(function(prev, curr, index, array) { console.log(prev +" "+ curr); return prev + curr; });

// Initial settings Index = 3, previous = initialValue = 4, current = 3
// The first iterationIndex = 2, previous = (4-3) = 1, current = 2 
// The second iterationIndex = 1, previous = (1-2) =-1, current = 1 
// The third iterationIndex = 0, previous = (-1 + 1) = 0, current = undefined (Exit) 
var reduceRight = [1,2,3,4].reduceRight(function(prev, curr, index, array) { if ( index == 0 ) { return prev + curr; } return prev - curr; });

console.log(reduceRight);


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.