javascript那些事兒(一)javascript數組用法總結(1)
/*1、js數組跟其他語言的數組一樣,都是資料的有序列表,但不同的是,js數組的每一項可以儲存任何類型的資料。並且js數組的大小是可以動態調整的。2、建立數組方式:*///(1)使用Array建構函式建立數組://var names = new Array();//建立length=30的數組//var names = new Array(30); //如果預Crowdsourced Security Testing道數組長度,可直接傳入數值 //alert(names.length); //30//也可以像Array建構函式傳遞數組應該包含的項://var names2 = new Array("zhangsan","lisi","wangwu");//alert(names2); //zhangsan,lisi,wangwu//給Array建構函式傳遞一個值也可以建立數組//var ages = new Array(2); //建立包含兩項的數組//var ages = new Array("11"); //建立包含1項,即字串“11”的數組//可以省略new操作符//var books = Array(2);//建立包含兩項的數組//var books = Array("books1");建立包含1項,即字串“books”的數組//(2) 使用數組字面量標記法建立數組:數組字面量由一對包含數組項的方括弧表示,多個數組項之間用逗號隔開//var names = ["zhangsan","lisi","wangwu"]; //建立一個包含3個字串的數組//var names = []; // 建立一個空數組//在數組最後一項添加逗號的結果://在其他瀏覽器中會建立包含2項且值分別為"zhangsan","lisi"的數組//在IE中,names會建立包含3項,值分別為“zhangsan","lisi",undefined的數組,//原因在於IE8及之前版本的ECMAScript實現在數組字面量方面存在BUG//var names = ["zhangsan","lisi",];//alert(names); //IE9下報錯,IE8及以下:zhangsan,lisi,//alert(names.length);//像這種省略值情況下,每一項都是undefined值。//var names = [,,,,,];//alert(names.length); //5:在及其他瀏覽器中建立包含5項的數組//alert(names.length); // 6: 在IE版本中建立包含6項的數組//alert(names[5]); //undefined//讀取設定數組值//var colors = ["red","green","blue"];//alert(colors[0]); //顯示第一項//colors[2] = "pink"; //修改第三項//alert(colors); // red,green,pink//colors[3] = "black"; //增加第四項,設定某個值的索引超過了數組長度,則數組長度自動加1//alert(colors); //red,green,pink,black//3、length屬性:>=0 ,length屬性不是唯讀,可以通過設定length屬性來從數組末尾移除項或向數組中添加項//var colors = ["red","green","blue"];//colors.length = 2; //alert(colors[2]); //undefined//var colors = ["red","green","blue"];//colors.length = 4; //alert(colors[3]); //undefined//var colors = ["red","green","blue"];//colors[colors.length] = "black";//colors[colors.length] = "pink";//alert(colors); //red,green,blue,black,pink//var colors = ["red","green","blue"]//colors[99] = "black";//alert(colors.length); // 100 ,colors[3]到color[98]都是undefined//4、數群組類型判斷 //對於一個網頁或一個全域範圍,instanceof假定單一的全域執行環境//var colors = ["red","green","blue"];//var obj = {};//alert(colors instanceof Array); //true//alert(obj instanceof Array); // false//對於網頁中包含多個架構,則就存在多個不同的全域執行環境,從而存在多個不同版本的Array建構函式,//那麼從一個架構向另一個架構傳入一個數組,則傳入的數組與第二個架構中的數組則分別具有不同構造//為瞭解決這個問題,則須採用Array.isArray()方法//var colors = ["red", "green", "blue"];//var obj = {};//alert(Array.isArray(colors)); //true//alert(Array.isArray(obj)); //false//相容性:支援isArray方法的瀏覽器有: IE9+,其他瀏覽器//5、toString(): 返回數組每一項用逗號分隔的字串,調用數組每一項的toString()方法//valueOf(): 返回數組//var colors = ["red", "green", "blue"];//alert("toString="+colors.toString()); //red,green,blue//alert(colors); //alert接收字串參數,傳遞給它的是數組,因此在後頭會調用Array的toString()方法//alert("valueOf="+colors.valueOf()); //alert(Array.isArray(colors.valueOf())); //true//toLocaleString(): 返回數組每一項用逗號分隔的字串,調用每一項的toLocaleString()方法,而不是toString()方法/*var book1 = {toString : function(){return "book1.toString()";},toLocaleString : function(){return "book1.toLocaleString()";}};var book2 = {toString : function(){return "book2.toString()";},toLocaleString : function(){return "book2.toLocaleString()";}};var book = [book1,book2];alert(book); //book1.toString(),book2.toString()alert(book.toString); //function toString(){[natice code]}alert(book.toString());//book1.toString(),book2.toString()alert(book.toLocaleString());//book1.toLocaleString(),book2.toLocaleString()*///join(): 接收一個作為分隔字元的參數, 返回由分隔字元分隔的字串,//var colors = ["red^", "green^", "blue^"];//alert(colors.join(','));//red^,green^,blue^//alert(colors.join('|'));//red^|green^|blue^//alert(colors.join()); //預設以逗號分隔: red^,green^,blue^//alert(colors.join(undefined));//在IE8+及其他瀏覽器中顯示:red^,green^,blue^//alert(colors.join(undefined)); //在IE7及以下版本顯示:red^undefinedgreen^undefinedblue^//如果數組中的某一項值為null或undefined,則該值在join()\valueOf()\toString()\toLocaleString()都返回Null 字元串//var names = ["zhangsan",null,undefined,"lisi"];//alert(names); //zhangsan,,,lisi//push(): 接收任意數量參數,添加到數組末尾,返回修改後數組長度//var names = new Array();//var num = names.push("zhangsan","lisi");//alert(num); //2//num = names.push("wangwu");//alert(num); //3//var item = names.pop();//alert(item); //wangwu//shift() : 移除數組第一項並返回//var colors = ["red","green","blue"];//var item = colors.shift(); //移除第一項並返回//alert(item);//alert(colors.length); //2//unshift() : 在數組前端添加任意個項並返回數組新長度,對於IE7及更早版本,unshift返回undefined而不是數組長度//var colors = ["red","green","blue"];//var count = colors.unshift("aaa","bbb");//alert(colors);//"aaa","bbb","red","green","blue"//alert(count);//5