The ECMASCRIPT5 Standard, released on December 3, 2009, brings some new ways to improve the operation of existing array arrays. (Note compatibility)
In ES5, a total of 9 array methods: http://kangax.github.io/compat-table/es5/
Array.prototype.indexofarray.prototype.lastindexofarray.prototype.everyarray.prototype.somearray.prototype.foreacharray.p Rototype.mapArray.prototype.filterArray.prototype.reduceArray.prototype.reduceRight
Here are 7 methods, the first 5 methods are common, many developers will use:
1, IndexOf ()
The IndexOf () method returns the position of the first element found in the array, or 1 if it does not exist.
vararr = [' Apple ', ' orange ', ' pear '], found=false;//not used for(vari = 0, L = arr.length; I < L; i++) { if(Arr[i] = = = ' Orange ') {found=true; }}console.log ("Found:", found);//==> found:true//after useConsole.log ("Found:", Arr.indexof ("orange")! =-1);//==> found:true
2. Filter ()
The filter () method creates an array of new matching filter conditions.
When no filter () is used:
var arr=[{"name": "Apple", "Count": 2},{"name": "Orange", "Count": 5},{"name": " Pear "," Count ": 3},{" name ":" Orange "," count ": +},]var newArr = []; for (var i = 0; i < arr.length; i++) { if (arr[i].name = = = "Orange") { NEWARR . push (Arr[i]);} } Console.log ("Filter results:", NEWARR);
The filter () is used:
var newArr = Arr.filter (function(item) { return item.name = = = "Orange";});
Console.log ("Filter results:", NEWARR);
3. ForEach ()
foreach executes the corresponding method for each element, which is used to replace the for loop.
var arr = [1, 2, 3, 4, 5, 6, 7, 8]; // using A For loop for (var i = 0, L = arr.length; i < L; i++) { console.log (arr[i]);} // Use the ForEach loop Arr.foreach (function(item, index) { console.log (item);});
4. Map ()
Map () After each element of the array is manipulated (mapped), a new array is returned.
varOldarr =[{first_name:"Colin", last_name:"Toh"}, {first_name:"Addy", last_name:"Osmani"}, {first_name:"Yehuda", last_name:"Katz"}];
functionGetnewarr () {varNEWARR = []; for(vari = 0; i < oldarr.length; i++) { varitem =Oldarr[i]; Item.full_name= [Item.first_name, Item.last_name].join (""); Newarr[i]=item; } returnNEWARR;}varPersonName =Getnewarr ();p Ersonname.foreach (function(item, index) {console.log (item);})/** * * output: Object {first_name: "Colin", Last_Name: "Toh", Full_name: "Colin Toh"}object {first_name: "Addy", last_name: "Osmani", Full_name: "Addy Osmani"}object {first_name: "Yehuda", Last_Name: "Katz", Full_name: "Yehuda Katz"}****/
Use the Map () method:
functionGetnewarr () {returnOldarr.map (function(item, index) {item.full_name= [Item.first_name, Item.last_name].join (""); returnitem; })}varPersonName =Getnewarr ();p Ersonname.foreach (function(item, index) {console.log (item);})/** * * output: Object {first_name: "Colin", Last_Name: "Toh", Full_name: "Colin Toh"}object {first_name: "Addy", last_name: "Osmani", Full_name: "Addy Osmani"}object {first_name: "Yehuda", Last_Name: "Katz", Full_name: "Yehuda Katz"}****/
5. Reduce ()
Reduce () can implement the function of an accumulator, lowering each value (from left to right) of the array to a value. It can also be understood as: Let the preceding and latter in the array do some kind of operation, and accumulate the final value;
Scene: Count the number of non-repeating words in an array;
var arr = ["Apple", "orange", "apple", "orange", "pear", "orange" ]; function getwordcnt () { var obj = {}; for (var i = 0; i < arr.length; i+ + var item = Arr[i]; Obj[item] = (Obj[item] + 1) | | 1 return obj; Console.log (getwordcnt ()); // output result: // Object {apple:2, orange:3, pear:1}
After using reduce ():
var arr = ["Apple", "orange", "apple", "orange", "pear", "orange"]; function getwordcnt () { return arr.reduce (function(prev, next) { = (Prev[next] + 1 ) || 1; return prev; }, {});} Console.log (getwordcnt ()); // Output Result: // Object {apple:2, orange:3, pear:1}
6, Array.some (callback[, Thisobject]); callback: functions are used to test certain elements. Thisobject: The object is used as the execution callback.
Detects if there are certain items in the array that meet the criteria;
var = [scores, max, +, +---]; var current =; function passed (score) { return score >// = = > True
7, Array.every (callback[, Thisobject]); callback: The function is used to test each element. Thisobject: The object is used as the execution callback.
Detects whether each item in the array meets the criteria;
var = [scores, max, +, +---]; var current =; function passed (score) { return score >// = = > False
A detailed example of the new array method in ECMASCRIPT5