引用类型-Array类型

ECMAScript数组的每一项可以保存任何类型的数据,并且数组的大小是可以动态调整的。

 

创建数组的基本方式有两种,第一种是使用Array构造函数

 

1         var colors = new Array();

 

创建一个含有二十项的数组

1        var colors = new Array(20);

 

创建一个包含三项的数组

1          var colors = new Array("pink","black","white");

 

当然也可以省略new操作符

1          var colors = Array(3);

 

创建数组的第二种基本方式是使用数组字面量表示法

1          var colors = ["red","blue","green"];

 

数组的项数保存在length属性中,他不是只读的,因此通过设置这个属性,可以从数组的末尾移除项或向数组添加新项

1          colors = 2;
2          console.log(colors[2]); //undefined

 

检测数组

 


对于一个网页或者一个全局作用于而言,使用instanceof操作符就能得到满意的结果

1         if(colors instanceof Array){//在不同的框架中传输时有问题!
2             alert("This is a Array");
3         }
4         

 

ECMAScript5中新增加的方法!(不用管数组是在哪个全局执行环境中创建的)

1         if(Array.isArray(colors)){//
2             alert("This is a Array");
3         }

 

转换方法

 


所有对象都具有toLocaleString()、toString()、valueOf()方法。其中,调用数组的toString()方法会返回由数组中每个值的字符串形式拼接而成的一个以逗号分割的字符串。而调用valueOf()返回的还是数组。实际上,为了创建这个字符串会调用每一项的toString()方法

1         var colors = ["red", "blue" ,"green"];
2         console.log(colors.toString());//red,blue,green 调用每一项的toString方法!
3         alert(colors.valueOf()); //red,blue,green 
4         alert(colors); //red,blue,green

 

toLocaleString()方法经常也返回与toString()和valueOf()方法相同的值,但也不总是如此。当调用数组的toLocaleString()方法时,他也会创建一个数组值的以都好分割的字符串,与之前不同的是他会调用每一项的toLocaleString()方法,而不是roString()方法

 1         var person1 = {
 2             toLocaleString:function(){
 3                 return "Nikolaos";
 4             },
 5             toString:function(){
 6                 return "Nicholas";
 7             }
 8         };
 9         var person2 = {
10             toLocaleString:function(){
11                 return "Grigorios";
12             },
13             toString:function(){
14                 return "Greg";
15             }
16         };
17          var people = [person1,person2];
18         alert(people);     // Nicholas,Greg 
19         console.log(people.toString());//Nicholas,Greg 
20         console.log(people.toLocaleString());//Nikolaos,Grigorios 

 

join()方法,可以使用不同的分隔符来构建这个字符串,如果不给join()方法传入任何值,或者给他传入undefined,则使用逗号作为分隔符

1         var colors = ["red", "blue" ,"green"];
2         console.log(colors.join("||")); //red||blue||green

 

栈方法

 


 

栈是一种LIFO(Last-In-First-Out)的数据结构,也就是最新添加的项最早被移除


push()方法可以接受任意数量的参数,把他们逐个添加到末尾,并返回修改后数组的长度


pop()方法从数组的末尾移除一项,减少数组的length值,并返回移除的项

 

1         var colors = new Array();
2         var count = colors.push("red","green");
3         alert(count);//2
4         var item = colors.pop();
5         alert(colors); //"red"
6         alert(item); //"green"
7                 

 

队列方法

 


 

队列数据结构的访问规则是FIFO(First-In-First-Out)。对列在列表的末端添加项,在列表的前端移除项

 

shift()能将数组的第一项移除并返回该项,同时数组的长度减1


unshift()会在数组的的前端加任意项,返回数组的长度

 

1         var colors = new Array();
2         var count = colors.push("red","green");
3         var item = colors.shift();
4          alert(item);// red
5         var item = colors.unshift("black","pink");
6         alert(item);// 3
7         alert(colors);//black pink  green

 

同时使用unshift和pop方法可以以相反的方向来模拟队列

1         var colors = new Array();
2         var count = colors.unshift("green","black");
3         alert(count);// 2
4         var count = colors.unshift("pink","white");
5         alert(count);//4
6         var item = colors.pop();
7         alert(item);// black

 

重排序方法

 


 

reverse()会反转数组的顺序


sort()会调用每个数组项的toString()方法,然后再按升序排列数组。sort() 可以接受一个比较函数作为参数

 

1         var num = [1,2,3,4,10,15];
2         num.reverse();
3         alert(num); //15,10,4,3,2,1
4 
5         num.sort();
6         alert(num);//1,10,15,2,3,4

 

简单的比较函数

 

 1         function compare(result1,result2){
 2             if(result1<result2){
 3                 return -1;
 4             }
 5             else if(result1>result2){
 6                 return 1;
 7             }
 8             else{
 9                 return 0;
10             }
11         }
12         var values = [0,1,5,9,7,10,11];
13         
14         console.log(values.sort(compare));// [0, 1, 5, 7, 9, 10, 11] 

 

更简单的方法

 

1         function compare(result1,result2){
2             return result1-result2;
3         }
4         
5           num.sort(compare);
6           alert(num);//1,2,3,4,10,15

 

操作方法

 


 

concat()会基于当前数组的所有项创建一个新的数组,具体来说就是,这个方法会创建当前数组的一个副本,然后将接收到的参数添加到这个副本的末尾,最后返回新构建的数组

 

1         var colors = ["red", "green","black"];
2         var colors2 = colors.concat("yellow","white");
3         alert(colors2); //red green black yellow white

 

slice()接受一或者两个参数,即要返回项的起始位置和结束位置,如果只有一个参数,slice()方法返回从该参数指定位置开始到当前数组末尾的所有项

 

1         var colors = ["red", "green","black"];
2         var colors3 = colors.slice(1,2);
3         alert(colors3); //green black

 

splice()的主要用途是向数组的中部插入项


splice()接受两个参数时:删除(删除第一项的位置,删除的项数)
       三个参数时:插入(插入的位置,0,插入的项1,插入的项2......)
       三个参数时:替换(替换的位置,1,加入的项)

 

1         var colors = ["red", "green","black"];
2         var colors4 = colors.splice(1,1);
3         alert(colors4); //green
4         alert(colors);//red black
5         var colors4 = colors.splice(1,0,"yellow");
6         alert(colors);// red yellow black 
7         var colors4 = colors.splice(1,1,"pink");
8         alert(colors);//red pink black

 

位置方法

 

indexOf()和lastIndexOf()接受两个参数(查找的项,查找开始的位置),其中indexOf()是从数组的开头开始向后查找,而lastIndexOf()是从数组的末尾开始向前查找。这两个方法返回要查找的项在数组当中的位置,或者在没有找到的情况下返回-1。在查找时会使用全等操作符

 

1         var numbers = [1,2,3,4,5,4,3,2,1];
2         var item = numbers.indexOf(4);
3         alert(item);//3
4         alert(numbers.lastIndexOf(4));//5
5         alert(numbers.indexOf(4,4));//5
6         
7         var person = {name:"Nicholas"};
8         var morePerson = [person];
9         alert(morePerson.indexOf(person));//0

 

迭代方法

 


ECMAScript5为数组定义了5个迭代方法,每个方法都接受两个参数:要在每一项上运行的函数和运行该函数的作用域对象-影响this的值,传入这些方法中的函数接受三个参数:数组项的值、该项在数组中的位置和数组对象本身

every():对数组中的每一项运行给定函数,如果该函数对每一项都返回true,则返回true

 

1         var numbers = [1,2,3,4,5,4,3,2,1];
2         var everyResult = numbers.every(function(item,index,array){
3              return (item>2);
4          })
5         alert(everyResult);//false

 

some():对数组中的每一项运行给定函数,如果该函数对任一项返回true,则返回true

 

1         var someResult = numbers.some(function(item,index,array){
2             return(item>2);
3         })
4         alert(someResult);//true

 

filter():对数组中的每一项运行给定函数,返回该函数会返回true的项组成的数组

 

1         var filterResult = numbers.filter(function(item,index,array){
2             return(item>2);
3         })
4         alert(filterResult); //3,4,5,4,3


map():对数组中的每一项运行给定函数,返回每次函数调用的结果组成的数组

 

1         var mapResult = numbers.map(function(item,index,array){
2             return(item*2);
3         })
4         alert(mapResult);//2,4,6,8,10,8,6,4,2

 

forEach():对数组中的每一项运行给定函数 

1         numbers.forEach(function(item,index,array){
2             //执行某些操作
3         })

 

归并方法

reduce()和reduceRight()这两个方法都会迭代所有数组的项,然后构建一个最终返回的值。其中,reduce()方法从数组的第一项开始,逐个遍历到最后,而reduceRight()正好相反,是从数组的最后一项开始,向前遍历到第一项。这两个方法都接受两个参数:一个在每一项上调用的函数和(可选的)作为归并基础的初始值。调用的函数接受4个参数:前一个值、当前值、项的索引和数组对象。第一次迭代发生在数组的第二项上

使用reduce()方法可以执行求数组中所有值之和的操作

 

 1         var results = [1,2,3,4,5];
 2         var sum = results.reduce(function(prev,cur,index,array){
 3             return prev+cur;
 4         }) ;
 5         alert(sum);//15
 6 
 7 
 8         var sum = results.reduceRight(function(prev,cur,index,array){
 9             return prec+cur;
10         }) 
11         alert(sum);//15

 

posted @ 2016-06-04 09:44  老板丶鱼丸粗面  阅读(199)  评论(0编辑  收藏  举报