ECMAscript5 new in-array functions

Source: Internet
Author: User

IndexOf ()
Format: Array. indexOf (item, start)
Function: Starting with the subscript, look for the item in the array for the first occurrence of the subscript.
Parameter: Item We're going to find the element
Start from which subscript to search, if not, the default starting from 0 to find
Return value: 1 not found
>=0 found the subscript.


var arr = ["Batman", "Superman", "Flash", "Green Lantern", "Wonder Woman", "Green Lantern"); var res = Arr.indexof ("Green Lantern", 4); alert (res); // 5 var arr = ["Batman", "Superman", "Flash", "Green Lantern", "Wonder Woman", "Green Lantern"); var res = indexOf (arr, "green" SSS); alert (res); // -1  not found


Custom (self-encapsulated function) indexOf

function indexOf (arr, item, start) {        var index =-1;   Suppose you can't find the        if(!  Start) {            = 0;        }      for (var i = start; i < arr.length; i++) {        if(arr[i] = = = Item            ) {=< c20> i;              Break ;        }    }     return index;}        

. ForEach (function (item, index, arr) {})

ForEach ()
Format: Array. ForEach (function (item, index, arr) {
Item currently traversed to the element
Index currently traversed by the element subscript
The current array of arr itself
})
Function: Used to iterate through an array.

For loop
For...in
Foreach

var arr = ["Batman", "Superman", "Flash", "Green Lantern", "Wonder Woman", "Green Lantern"];arr.foreach (function+ "," +  index);})

Effect:

Traditional way

 for (var i = 0; i < arr.length; i++) {alert (arr[i]);}


Map () traversal

var arr = [Ten, +, +, +]; var newArr = Arr.map (function(item, index, arr) {  return//  Newarr the result of the subscript }) alert (arr); // Ten, +, (NEWARR); // 13,26 ,-------



Filter () Filtering

  var arr = [Ten, +, +, +]; var newArr = Arr.filter (Item, index, arr) {  // finds all elements greater than 20 in the original array, Generate a new array return// filter Condition }); alert (arr); // Ten, +, (NEWARR); // it's A.  

/*
Some () some


Determine if there are elements that meet the criteria.

Note: Simply exit the loop as soon as you find the first element that meets the criteria.
Return value: True False

var arr = [Ten, +, +, +]; var res = arr.some (function(item, index, arr) {    //  Alert (item + "," + index);     // conditions of judgment.     return item >;}) Alert (res); // it's A.

Every ()
Determines whether all elements meet the criteria.

"Note" To find the first element that does not meet the criteria, terminate the loop and return False

Return value: True False


var arr = [Ten, +, +, +]; var res = arr.every (function(item, index, arr) {    + "," + index ")    ; // judge the condition.     return item <;}) Alert (res); // false

Modify the judging conditions in the above code to:

return item > 5;

The result is true:

Alert (res); // true

Reduce () Merge

Syntax: array. reduce (function (prev, next, index, arr) {})

Prev The result of an expression after the last loop return
Next the element that is traversed
Index this time the subscript of the traversed element

var arr = [Ten, +, +, +]; var res = arr.reduce (function(prev, Next, index, arr) {/*prev Last loop  Return the result of the expression after next this time traversing the element index this time traversing the subscript of the element */+ "," + Next ";  return prev + Next;}) Alert (res); // 150 The sum of the array elements is realized by this method


ECMAscript5 new in-array functions

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.