?<wbr></wbr>?<wbr></wbr>?<wbr></wbr>JavaScript 数组使用技巧
?<wbr></wbr>?<wbr></wbr>
?<wbr></wbr>?<wbr></wbr>?<wbr></wbr>//创建数组, 长度由后期赋值决定
?<wbr></wbr> ?<wbr></wbr> var arry1 = new Array;
?<wbr></wbr> ?<wbr></wbr> for (i = 0; i < 5; i++) {
?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr>?<wbr></wbr>arry1 = 2 * i + 1;
?<wbr></wbr> ?<wbr></wbr> }
?<wbr></wbr> ?<wbr></wbr> // length属性, 返回数组的长度
?<wbr></wbr> ?<wbr></wbr> alert("arry1.length=" + arry1.length);
?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> // toString()方法, 返回数组的字符串表示
?<wbr></wbr> ?<wbr></wbr> alert("arry1=" + arry1.toString());
?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> // 构建数组并初始化, 有点像Java
?<wbr></wbr> ?<wbr></wbr> arry2 = [2, 4, 6, 8];
?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> // concat方法, 用于连接两个数组
?<wbr></wbr> ?<wbr></wbr> var arry3 = arry1.concat(arry2);
?<wbr></wbr> ?<wbr></wbr> alert(arry3); // 1, 3, 5, 7, 9, 2, 4, 6, 8
?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> // concat的另外一种用法, 可以有多个参数
?<wbr></wbr> ?<wbr></wbr> var arry4 = arry2.concat(1, 3, 5, 7, 9);
?<wbr></wbr> ?<wbr></wbr> alert(arry4); // 2, 4, 6, 8,?<wbr></wbr>?<wbr></wbr>1, 3,?<wbr></wbr>?<wbr></wbr>5,?<wbr></wbr>?<wbr></wbr>7,?<wbr></wbr>?<wbr></wbr>9
?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> // join方法, 用于将数组中的各个元素连接成字符串
?<wbr></wbr> ?<wbr></wbr> var arry5 = ["one", "two", "three"];
?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> // 默认用","连接
?<wbr></wbr> ?<wbr></wbr> var strArry = arry5.join();
?<wbr></wbr> ?<wbr></wbr> alert(strArry); // "one,two,three"
?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> // 也可以指定连接的字符
?<wbr></wbr> ?<wbr></wbr> strArry = arry5.join("|");
?<wbr></wbr> ?<wbr></wbr> alert(strArry); // "one|two|three"
?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> // sort 方法用于将数组排序
?<wbr></wbr> ?<wbr></wbr> var arry6 = [2, 3, 1, 6, 5, 3, 1, 4, 7];
?<wbr></wbr> ?<wbr></wbr> arry6.sort();
?<wbr></wbr> ?<wbr></wbr> alert(arry6.toString()); // 1, 1, 2, 3, 3, 4, 5, 6, 7
?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> // 对于字符串数组, sort方法使用字典的顺序排序
?<wbr></wbr> ?<wbr></wbr> var arry7 = ['BB', 'AAA', 'C'];
?<wbr></wbr> ?<wbr></wbr> arry7.sort();
?<wbr></wbr> ?<wbr></wbr> alert(arry7); // AAA, BB, C
?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> // 也可以通过回调函数的方式自定义排序的大小逻辑
?<wbr></wbr> ?<wbr></wbr> // 例如下面的例子将按照字符串的长度对arry7排序
?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> arry7.sort(function(a1, a2) {
?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr> return a1.length - a2.length;
?<wbr></wbr> ?<wbr></wbr> });
?<wbr></wbr> ?<wbr></wbr> alert(arry7); // C, BB, AAA
?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> // 调用reverse方法, 可以将数组反转
?<wbr></wbr> ?<wbr></wbr> arry8 = [1,2,3,4,5];
?<wbr></wbr> ?<wbr></wbr> arry8.reverse();
?<wbr></wbr> ?<wbr></wbr> alert(arry8.toString()); // 5, 4, 3, 2, 1
?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> // slice方法, 用于截取数组的一部分并以数组的形式返回
?<wbr></wbr> ?<wbr></wbr> // 两个参数分别为截取部分的上界和下界(前包括, 后不包括, 和Java一样)
?<wbr></wbr> ?<wbr></wbr> var arry9 = ['一','二','三','四','五'];
?<wbr></wbr> ?<wbr></wbr> var arry10 = arry9.slice(1,3);
?<wbr></wbr> ?<wbr></wbr> alert(arry10); // 二, 三
?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> // splice方法, 用于删除原数组的一部分内容, 并用指定的元素替换
?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> var arry11 = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
?<wbr></wbr> ?<wbr></wbr> arry11.splice(1, 5, '&','%','#');
?<wbr></wbr> ?<wbr></wbr> alert(arry11); // A, &, %, #, G
?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> // pop和push方法, 可以利用数组实现栈(先进后出)的操作
?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> var arry12 = new Array;
?<wbr></wbr> ?<wbr></wbr> // 进栈一个元素
?<wbr></wbr> ?<wbr></wbr> arry12.push('one');
?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> // 进栈多个元素
?<wbr></wbr> ?<wbr></wbr> var size = arry12.push('two','three','four');
?<wbr></wbr> ?<wbr></wbr> alert(size); // 4
?<wbr></wbr> ?<wbr></wbr> size = arry12.push('five','six','seven','eight','night','ten');
?<wbr></wbr> ?<wbr></wbr> alert(size); // 10
?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> for(i=0; i<size; i++) {
?<wbr></wbr> ?<wbr></wbr>?<wbr></wbr>?<wbr></wbr>alert(arry12.pop());
?<wbr></wbr> ?<wbr></wbr> }
?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> //Prototype的clone方法, 用于实现数组的复制(克隆), 返回值为复制后的新数组
?<wbr></wbr> ?<wbr></wbr> var str = "how long no see you";
?<wbr></wbr> ?<wbr></wbr> // string的split方法有点像Java语言
?<wbr></wbr> ?<wbr></wbr> var arry13 = str.split(' ');
?<wbr></wbr> ?<wbr></wbr> alert(arry13); // how, long, no, see, you
?<wbr></wbr> ?<wbr></wbr> var arry14 = arry13.clone();
?<wbr></wbr> ?<wbr></wbr> alert(arry14); // how, long, no, see, you
?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> // Prototype的compact方法, 用于删除原数组中值为null和undefined(未定义)的元素
?<wbr></wbr> ?<wbr></wbr> // 返回值为删除后的新数组, 原数组不变
?<wbr></wbr> ?<wbr></wbr> var arry15 = ["spring",,"summer",,null,"autumn",,,"winter",null];
?<wbr></wbr> ?<wbr></wbr> alert(arry15.length); //10:
?<wbr></wbr> ?<wbr></wbr> var arry16 = arry15.compact();
?<wbr></wbr> ?<wbr></wbr> alert(arry16.length+": "+arry16.toString()); //4: spring, summer, autumn, winter
?<wbr></wbr> ?<wbr></wbr>
?<wbr></wbr> ?<wbr></wbr> // Prototype的without方法, 用于删除数组中的指定元素
?<wbr></wbr> ?<wbr></wbr> // without方法支持多参数, 返回值为删除后的新数组, 原数组不变
?<wbr></wbr> ?<wbr></wbr> var arry17 = ['java', 'html', 'css', 'c++','js','php','c#','js'];
?<wbr></wbr> ?<wbr></wbr> // 删除数组arry18中值为'html', 'css' 和'js'的元素
?<wbr></wbr> ?<wbr></wbr> var arry18 = arry17.without('html','css','js');
?<wbr></wbr> ?<wbr></wbr> alert(arry18.toString()); // java, c++, php, c#