ES5 in array new API interface ForEach map filter Some every indexOf lastIndexOf reduce reduceright

Source: Internet
Author: User

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

ForEach: Loops, iterates through an array, no return value. Similar to For Loop
Array.foreach (function (value, index, array) {  console.log (value);  return value *  value;});

Map : The map means that the map returns a new array with a return value; Filterarr: Returns a new object
var Maparr = Array.map (function (value, index, array) {return value *  value;});

Filter : The meaning of filtering, filtering, return value; Filterarr: Returns the newly filtered array
var Filterarr = Array.filter (function (value, index, array) {  if (value >) {    return true;  }  return false;});

Everyarr: Every one, every meaning; return value; every: Returns Boolean true or false;
var Everyarr = array.every (function (value, index, array) {  if (value > 1) {    return true;  }});

IndexOf: The first one is the retrieved value, the second is the location to retrieve it from 0 onwards
Console.log (Array.indexof (23,0));

LastIndexOf: The first is the retrieved value, the second is the location to be retrieved from Arr.length-1
Console.log (Array.lastindexof (2,10));

//Initial setupprevious = InitialValue = 1, current = 2 
//First Iteration previous = (1 + 2) =  3, current = 3  
//Second Iteration Previous = (3 + 3) =  6, current = 4  
//Third Iteration  Previous = (6 + 4) =  Ten, current = undefined (exit)
var reduce = [1,2,3,4].reduce (function (prev, Curr, index, array) {  Console.log (prev + "  + Curr);  return prev + Curr;});

//Initial setup index = 3, previous = InitialValue = 4, current = 3  
//First Iteration index = 2, previous = (4-3) = 1, current = 2  
//Second Iteration index = 1, previous = (1-2) =-1, current = 1  
//Third Iteration  index = 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 + cur R;  }  return prev-curr;});

Console.log (Reduceright);


ES5 in array new API interface ForEach map filter Some every indexOf lastIndexOf reduce 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.