I. Location method
ECMAScript5 adds two locations to an array instance: IndexOf () and LastIndexOf (). Both methods receive two parameters: the subparagraphs (optional) to find represents the index at which to find the starting point (as in array [7,8,9,1,0], "7" in the first position, and its index is 0. )。 where the IndexOf () method starts looking backwards from the beginning of the array (position 0), the LastIndexOf () method looks forward from the end of the array.
Attention:
Both methods return the position of the item being looked up in the array, returning 1 without finding it.
The congruent operator (= = =) is used when comparing the first argument to each item in the array.
eg
1 var numbers=[1,2,3,4,5,4,3,1]; 2 Alert (Numbers.indexof (4)),//3 3 alert (Numbers.lastindexof (4)),//4 4 5 alert (numbers.indexof (bis)),//5 6 alert ( Numbers.lastindexof (bis));//3 7 8 var person={name: "Linshuling"}; 9 var people=[{name: "Linshuling"}];10 one var MOREPEOPLE=[PERSON];12 Alert (People.indexof (person)),//-114 alert (morepeople.indexof (person));//0
Two. Iterative methods
ECMASCRIPT5 defines 5 iterative methods for an array. Each method receives two parameters: the function that runs on each item and (optionally) the scope object that runs the function-the value that affects this.
The functions passed in these methods receive three parameters: the value of the array item, the position of the item in the array, and the object itself.
(1) every (): Each item in an array runs the given function, and returns TRUE if the function returns true for each item.
eg
1 var numbers=[1,2,3,4,5,4,3,2,1];2 var everyresult=numbers.every (function (Item,index,array) {3 return (item> 2); 4}) 5 var everyresult1=numbers.every (function (Item,index,array) {6 return (item>0); 7}) 8 Alert (Everyresult) ;//false9 alert (EVERYRESULT1);//true
(2) Some (): Runs the given function for each item in the array, and returns True if the function returns true for any of the entries.
eg
1 var numbers=[1,2,3,4,5,4,3,2,1];2 var someresult=numbers.some (function (Item,index,array) {3 return (ITEM>4) ; 4}) 5 var someresult1=numbers.some (function (Item,index,array) {6 return (ITEM>5); 7}) 8 alert (someresult);// TRUE9 alert (SOMERESULT1);//false
(3) filter () runs the given function for each item in the array, and returns a list of the items that the function returns True.
eg
1 var numbers=[1,2,3,4,5,4,3,2,1];2 var filterresult=numbers.filter (function (Item,index,array) {3 return (item >4); 4}) 5 var filterresult1=numbers.filter (function (Item,index,array) {6 return (ITEM>5); 7}) 8 Alert ( Filterresult);//59 alert (FILTERRESULT1);//Empty array
(4) Map () runs the given function for each item in the array, and returns a list of the results of each function call.
eg
1 var numbers=[1,2,3,4,5,4,3,2,1];2 var mapresult=numbers.map (function (Item,index,array) {3 return (ITEM+4); 4}) 5 var mapresult1=numbers.map (function (Item,index,array) {6 return (ITEM>5); 7}) 8 alert (mapresult);// 5,6,7,8,9,10,9,8,7,6,59 alert (MAPRESULT1);//false,false,false,false,false,false,false,false,false
(5) ForEach () runs the given function for each item in the array. This function has no return value.
eg
1 var numbers=[1,2,3,4,5,4,3,2,1];2 var foreachresult=numbers.foreach (function (Item,index,array) {3 return (item +4); 4}) 5 var foreachresult1=numbers.foreach (function (Item,index,array) {6 return (ITEM>5); 7}) 8 Alert ( Foreachresult);//undefined9 alert (FOREACHRESULT1);//undefined
This method is essentially the same as using the For loop iteration group.
eg
1 var numbers=[1,2,3,4,5,4,3,2,1];2 Number.foreach (function (Item,index,array) {3 //Perform some operations 4 });
Three. Merge method
There are two new methods for merging arrays in ECMASCRIPT5: Reduce () and reduceright (). The two methods iterate over all the items of an algebraic group and then build a final return value. where the reduce () method starts with the first item in the array, and then it passes through to the end. The Reduceright () begins with the last item in the array, moving forward through the first item.
Both methods receive two parameters: one is the function that is called on each item and (optionally) the initial value that is the base of the merge. The functions passed to reduce () and reduceright () receive four parameters: the previous value, the current value, the index of the item, and the array object.
eg
You can use the reduce () method to perform all the values in the array and
1 var number=[1,2,3,4,5];2 var sum=number.reduce (function (Prev,cur,index,array) {3 return prev+cur;4}); 5 alert ( sum);//15
Note: Using reduce and reduceright () depends primarily on which header you want to start the calendar over. Besides, they are exactly the same.
JS elevation 5. Reference type (6) Location method of array type, iterative method, merge method