Array類型
"可以保存任何類型的數(shù)據(jù),并且大小可以調(diào)整的.“
一碰凶、創(chuàng)建實例
1暮芭、使用Array構(gòu)造函數(shù)
var color = new Array();
2、使用數(shù)組字面量表示法
var color = ["red","blue","yellow"];
- 其中需要注意的是:強烈建議不要使用 var option = [,,,,];
3欲低、元素的讀取和設(shè)置使用【】+下標(biāo)
4辕宏、數(shù)組長度使用.length
二、檢測數(shù)組
對于一個網(wǎng)頁或者一個全局作用域而言砾莱,使用instanceof操作符就能得到滿足的結(jié)果瑞筐,但是對于網(wǎng)頁包含多個框架,從而會存在兩個以上不同版本的Array構(gòu)造函數(shù)腊瑟。所以引進(jìn)了Array.isArray()方法聚假,具體用法如下:
if(Array.isArray(value)) { }
三、轉(zhuǎn)換方法
如前所述闰非,所有對象都具有toLocaleString()膘格、toString()和valueOf()方法。
1河胎、toString()方法:返回由數(shù)組中每個值得字符串形式拼接而成的一個以逗號分隔的字符串闯袒;
2虎敦、toLocaleString()方法:經(jīng)常也會返回與toString()和valueOf()方法相同的值游岳,但唯一不同之處在于:為了取得每一項的值,調(diào)用的是每一項的toLocaleString()方法其徙,而不是toString()方法,請看下面的例子
var person1 = {
toLocaleString : function () {
return "Nikolaos";
},
toString : function() {
return "Nicholas";
}
};
var person2 = {
toLocaleString : function () {
return "Grigorios";
},
toString : function() {
return "Greg";
}
};
var people = [person1,person2];
alert(people); //Nicholas,Greg
alert(people.toString()); //Nicholas,Greg
alert(people.toLocaleString()); //Nikolaos,Grigorios
4胚迫、前三種方法,在默認(rèn)情況下都會返回以逗號分隔的字符串唾那。而如果使用join()方法访锻,則可以使用不同的分隔符來構(gòu)建這個字符串褪尝。join()方法只接受一個參數(shù),即用作分隔符的字符串期犬,然后返回包含所有數(shù)組項的字符串河哑。
四、棧和隊列方法
1龟虎、棧方法
function Stack(){
var items=[];//用數(shù)組來保存棧中的數(shù)據(jù);
this.push=function(element){
items.push(element);
};
this.pop=function(){
return items.pop();
};
this.peek=function(){
return items[items.length-1];
};
this.isEmpty=function(){
return items.length == 0;
};
this.size=function(){
return items.length;
}
this.clear=function(){
items=[];
}
this.toString=function(){
return items.toString();
};
}
主要是兩個方法:push():添加到數(shù)組的末尾璃谨;pop():從末尾移除最后一項;
2鲤妥、隊列方法
function Queue(){
var items=[];//用數(shù)組來保存棧中的數(shù)據(jù);
this.enqueue=function(element){
items.push(element);
};
this.dequeue=function(){
return items.shift();
};
this.font=function(){
return items[0];
};
this.isEmpty=function(){
return items.length == 0;
};
this.size=function(){
return items.length;
}
this.clear=function(){
items=[];
}
this.toString=function(){
return items.toString();
};
}
主要兩個方法:push()方法和shift()方法:從數(shù)組的前端取得項佳吞;
五、重排序方法
1棉安、reverse()方法:返回數(shù)組項的逆序底扳;
2、sort()方法:
sort(function compare(value1,value2){
return value1 - value2; //升序
return value2 - value1; //降序
});
六贡耽、操作方法
1衷模、concat()方法可以基于當(dāng)前數(shù)組中的所有項創(chuàng)建一個新數(shù)組,將接收到的參數(shù)添加到這個副本的末尾菇爪;
var colors = ["red","green","blue"];
var colors2 = colors.concat("yellow",["black","brown"]);
alert(colors2); // red,green,blue,yellow,black,brown;
2算芯、slice()方法可以基于一個或多個項創(chuàng)建一個新數(shù)組〉手妫可以接收兩個參數(shù)熙揍,一個起始位置,一個結(jié)束為止氏涩,當(dāng)只有一個參數(shù)時届囚,從起始位置到數(shù)組末尾項;
var colors = ["red","green","blue","yellow"];
var colors2 = colors.slice(1);
var colors3 = colors.slice(1,2);
alert(colors2); //green,blue,yellow;
alert(colors3); // green,blue;
3是尖、splice()方法:刪除意系、插入、替換饺汹,返回一個包含從原始數(shù)組中刪除的項的數(shù)組蛔添。
刪除:splice(起始位置,刪除項數(shù))兜辞;
插入:splice(起始位置迎瞧,0,...逸吵,...);
替換:splice(起始位置凶硅,1,...扫皱,...);
七足绅、位置方法
1捷绑、indexOf(起始位置,尋找的元素):從數(shù)組開頭開始向后找氢妈;
2粹污、lastIndexOf(起始位置,尋找的元素):從數(shù)組末尾開始向前找首量;
這兩個方法都返回要查找的項在數(shù)組中的位置厕怜,或者在沒有找到的情況下返回-1;
八蕾总、迭代方法
ECMAScript5為數(shù)組定義了5個迭代方法粥航。每個方法都接受兩個參數(shù):要在每一項上運行的函數(shù)和(可選的)運行該函數(shù)的作用域?qū)ο?-影響this的值。傳入這些方法中的函數(shù)會接受三個參數(shù):數(shù)組項的值生百、該項在數(shù)組中的位置和數(shù)組對象本身递雀。
1、every(function(item,index,array){}) 每一項都返回true蚀浆,結(jié)果才為true
var numbers = [1,2,3,4,5,4,3,2,1];
var everyResult = numbers.every(function(item,index,array){
return (item>2);
});
alert(everyResult); //false;
2缀程、some(function(item,index,array){}) 對任一項返回true,結(jié)果就為true市俊;
var someResult = numbers.some(function(item,index,array){
return (item>2);
});
alert(someResult); //true;
3杨凑、filter(function(item,index,array){}) 返回該函數(shù)會返回true的項組成的數(shù)組
var numbers = [1,2,3,4,5,4,3,2,1];
var filterResult = numbers.filter(function(item,index,array){
return (item>2);
});
alert(filterResult); //[3,4,5,4,3];
4、forEach(function(item,index,array){}) 對數(shù)組每一項執(zhí)行函數(shù)摆昧,沒有返回值
5撩满、map(function(item.index,array){}) 返回每次函數(shù)調(diào)用結(jié)果的數(shù)組
var numbers = [1,2,3,4,5,4,3,2,1];
var mapResult = numbers.map(function(item,index,array){
return item*2;
});
alert(mapResult); //[2,4,6,8,10,8,6,4,2];
九、歸并方法
ECMAScript5還信增了兩個歸并數(shù)組的方法:reduce()和reduceRight()绅你。這兩個方法都會迭代數(shù)組的所有項伺帘,然后構(gòu)建一個最終返回的值。這連個方法都接收兩個參數(shù):一個在每一項上調(diào)用的函數(shù)和(可選)作為歸并基礎(chǔ)的初始值忌锯。傳入的函數(shù)接收4個參數(shù):前一個值伪嫁、當(dāng)前值、項的索引和數(shù)組對象本身偶垮。
1张咳、reduce(function(prev,cur,index,array){}),從數(shù)組的第一項開始似舵,逐個遍歷到最后脚猾。使用reduce()方法可以執(zhí)行求數(shù)組中所有值之和的操作。比如:
var values = [1,2,3,4,5];
var sum = values.reduce(function(prev,cur,index,array){
return prev + cur;
});
alert(sum); //15;
2啄枕、reduceRight(function(prev,cur,index,array){}) 從數(shù)組的最后一項開始hi婚陪,向前遍歷到第一項