第五章:引用类型

1:Array.isArray()方法

这个方法的目的是确定某个值是不是数组

if(Array.isArray(value)){

}

 

2:栈方法

push()和pop()方法

// 例子
        var colors=["red","blue"];
        colors.push("brown");
        colors[3]="black";
        alert(colors.length);  //4

        var item=colors.pop();
        alert(item);        //black

 

3:队列方法

push()和shift()方法

// 例子
         var colors=new Array();
        var count=colors.push("red","green");
        alert(count);
        count=colors.push("black");
        alert(count);
        var item=colors.shift();
        alert(item);    //red
        alert(colors.length);

 

unshift()方法

unshift()方法和shift()方法的用途相反,它能在数组前端添加任意个项并返回新数组的长度。因此,同时使用unshift()和pop()方法,可以从相反的方向模拟队列,即在数组的前端添加项,数组的末端移除项

        var colors=new Array();
        var count=colors.unshift("red","green");
        alert(count);

        count=colors.unshift("black");
        alert(count);

        var item=colors.pop();
        alert(item);    //green
        alert(colors.length);

 

4:重排序方法

数组中存在两个直接排序的方法 reverse()和sort()

reverse()方法会反转数组项的顺序

var values=[1,2,3,4,5];
values.reverse();
alert(values);    //5,4,3,2,1

 

默认情况下,sort方法按照升序排列数组项,sort()方法调用每个数组项的toString()转型方法,sort()方法比较的也是字符串

var values=[0,25,5,33,15];
values.sort();
alert(values);   //0,15,25,33,5

 

这样的比较不正确,因此需要sort()方法接受一个比较函数作为参数

function compare(a,b){
        if (a>b) {
            return 1;
        } else if (a<b) {
            return -1;
        } else {
            return 0;
        }
    }

    var values = [0, 25, 5, 33, 15];
    values.sort(compare);
    alert(values); //0,5,15,25,33

 

如果是降序,交换compare中的数值即可。

或者更快的方法是在上述的基础上,利用reverse()方法

 

5:操作方法

*concat()方法,连接

         var colors=["red","green","blue"];
         var colors2=colors.concat("yellow",["black","blown"]);
         alert(colors);    //red,green,blue
         alert(colors2);   //red,green,blue,yellow,black,brown

 

*slice()方法,基于当前数组中的一个或多个项创建一个新数组

    var colors=["red","green","blue","yellow","purple"];
    var colors2=colors.slice(1);    //green,blue,yellow,purple
    var colors3=colors.slice(1,4);  //green,blue,yellow
    alert(colors2);
    alert(colors3);

 

如果slice()方法的参数中存在负数,则用数组长度加上该数组来确定相应的位置;例如,包含5项的数组上调用slice(-1,-2)与调用slice(4,3)得到的结果相同。如果结束位置小于开始位置,则返回 空数组

 

*splice()方法

(1) 删除:可以删除任意数量的项,只需指定2个参数:要删除第一项的位置和要删除的项数

        例如:splice(0,2)会删除数组中的前两项

    var colors=["red","green","blue"];
    var removed=colors.splice(0,1);  
    alert(colors);          //green,blue
    alert(removed);         //red

(2) 插入:可以向指定位置插入插入任意数量的项,只需提供3个参数:起始位置,0(要删除的项数),和要插入的项。如果要插入多个项,可以再传入第四第五任意多个项。例如,splice(2,0,“red”,"green")会从当前位置2开始插入字符串“red”“green”

 var colors = ["red", "green", "blue"];
 var removed = colors.splice(1, 0, "yellow", "orange");
 alert(colors); //red,yellow,orange,green,blue
 alert(removed); //返回一个空数组

(3) 替换:可以向指定位置插入任意的项,且同时删除任意数量的项,只需指定3个参数:起始位置、要删除的项数、要插入的任意数量的项。插入的项数不必与删除的项数相等,例如,splice(2,1,“red”,“green”)会删除当前数组位置2的项,然后再从位置2开始插入字符串“red”,“green”

var colors = ["red", "green", "blue"];
var removed=colors.splice(1,1,"yellow","orange");
alert(colors);      //red,yellow,orange,blue
alert(removed);     //green2

 

6:位置方法

indexOf()方法从数组的开头向后查找

lastIndexOf()方法从数组的尾部开始查找

这两个方法都接收两个参数,要查找的项和起点位置的索引

var numbers=[1,2,3,4,5,4,3,2,1];
alert(numbers.indexOf(4));  //3
alert(numbers.lastIndexOf(4));  //5

alert(numbers.indexOf(4,4));  //5
alert(numbers.lastIndexOf(4,4));  //3

var person={name:"yin"};
var people=[{name:"yin"}];

var morePeople=[person];

alert(people.indexOf(person)); //-1
alert(morePeople.indexOf(person));  //0

 

7:迭代方法

*every():对数组中的每一项运行给定函数,如果该函数的每一项都返回true,则返回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

 

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

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]

 

*forEach():对数组中的每一项运行给定函数,这个方法没有返回值

var numbers=[1,2,3,4,5,4,3,2,1];
var forEachResult=numbers.forEach(function(item,index,array){
    //执行某些操作
})

 

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

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]

 

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

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

 

8:缩小方法

reduce() 和 reduceRight()

reduce() 方法是从数组的第一项开始,逐个遍历到最后

reduceRight()方法是从数组的最后一项开始,向前遍历到第一项

posted @ 2016-03-20 21:50  殷的博客-Exploration  阅读(152)  评论(0编辑  收藏  举报