Javascript Array类小结

Array类:

首先

数组定义习惯上都用 [], 下面分别使用 new Array() [] ,验证时间效率代码如下,看看谁用的时间少:

var a = new Date ;   
var arr, direct, b 
= [];   
for(var i=0; i<10000; i++)   
 arr 
= new Array();   
b.push(
new Date() - a);   
= new Date;   
for(var i=0; i<10000; i++)    
direct 
= [];   
b.push(
new Date() - a);   
alert(b.join(
" : "));  

 

以下总结参考 JS 高级程序设计

1 创建 Array 类对象

(1)
var numbers
= new Array();//习惯上是var numbers= [];   
numbers [0= "one";   
numbers [
2= "two";   
numbers [
3= "three";   
numbers [
35= "four";   
alert(numbers [
0]);     //outputs “one”   
alert(numbers [35]);   //outputs ”four”   
alert(numbers.length);  //outputs “36” 

 

(2)   
var numbers
= new Array(15);// 习惯上是var numbers= [15];   
alert(numbers [0]);        //outputs "undefined"   
alert(numbers.length);    //outputs "15"  

 

(3)   
var numbers
= new Array("one","two","three");//习惯上是var numbers= ["one","two","three"];   
alert(numbers [0]);      //outputs "undefined"   
numbers [10= "four";   
alert(numbers.length);    
//outputs "11"  

 

(4)用字面量表示定义Array对象:   
var numbers
= ["one","two","three"];   
alert(numbers [
0]);      //outputs "undefined"   
numbers [10= "four";   
alert(numbers.length);     
//outputs "11"  

 

2 Array 对象覆盖 toString() 方法和 valueOf() 方法,返回字符串

var numbers= ["one","two","three"];   
alert(numbers.toString());  
//outputs "one,two,three"   
alert(numbers.valueOf());  //outputs "one,two,three"  

 

3 toLocalString() 方法

var numbers= ["one","two","three"];   
alert(numbers.toLocaleString());  
//outputs "one,two,three"  

 

4 join() 方法,用来连接字符串的值

var numbers= ["one","two","three"];   
alert(numbers.join(
","));  //outputs "one,two,three"   
alert(numbers.join("-and-"));  //outputs "one-and-two-and-three"   
alert(numbers.join(""));  //outputs "onetwothree"   
alert(numbers.join(")("));  //outputs "one)(two)(three"  

 

 5 concat() 方法

var numbers= ["one","two","three"];   
var arr 
= numbers;   
var numbers2
=arr.concat("four","five");   
alert(numbers2.toString());     
//outputs "one,two,three,four,five"   
alert(numbers.toString());     //outputs "one,two,three"   
//alert(numbers.concat("four","five"));   //outputs "one,two,three"  

 

6 slice() 方法

var numbers = ["one","two","three","four","five","six"];   
var arr 
= numbers;   
var numbers2 
= arr.slice(1);   
alert(numbers2.toString());   
//outputs "two,three,four,five,six"   
var numbers3 = arr.slice(1,4);   
alert(numbers3.toString());   
//outputs "two,three,four" 

 

7 调用 push() pop() 方法实现后进先出的结构(栈 stack

var stack= ["one","two","three"];   
stack.push(
"four");   
stack.push(
"five","six");   
alert(stack.toString());       
//outputs "two,three,four,five,six"   
  
var removelast
=stack.pop();   
alert(removelast);          
//outputs "six"   
var removelast=stack.pop();   
alert(removelast);           
//outputs "five"   
alert(stack.toString());        //outputs "two,three,four"  

 

8 调用 shift() push() 方法实现后进后出结构(队列 queue

var queue= ["one","two","three"];   
queue.push(
"four");   
alert(queue.toString());         
//outputs "one,two,three,four"   
  
var removefirst 
= queue.shift();   
alert(removefirst);           
//outputs "one"   
alert(queue.toString());       //outputs "two,three,four"  

 

其它

    (1) unshift() 方法把一个项添加在数组的第一个位置

(代码的上面部分可以用 8 的代码)

var addfirst = queue.unshift("one");   
//alert(addfirst);             //outputs "4"   
alert(queue.toString());       //outputs "one,two,three,four"  

(2) reverse() 方法颠倒数组项顺序

  (代码衔接上面的 unshift() )

queue.reverse();    
alert(queue.toString());        
//outputs "four,three,two,one"  

    (3) sort() 方法按数组项的升序排列

var queue= ["one","two","three"];   
queue.sort();   
alert(queue.toString());    
//outputs "one,three,two"  

    (4) splice() 方法。第一参数是指起始位置,第二个参数是要删除项的个数,第三个参数是要插入的项

var queue= ["one","two","three","four","five"];   
queue.splice(
0,2);   
alert(queue.toString());        
//outputs "three,four,five",delete one and two   
  
queue.splice(
1,1,"and1","and2","and3");   
alert(queue.toString());         
//outputs "three,and1,and2,and3,four" 

 

 

 

 

posted @ 2010-03-30 11:24  吴庆阳  阅读(275)  评论(0编辑  收藏  举报