ES5-Array的新增方法

Array.prototype.indexof(value):得到值在数组中的第一个下标

Array.prototype.lastIndexof(value):得到值在数组中的最后一个下标

Array.prototype.foreach(function(item,index){}):遍历数组

Array.prototype.map(function(item,index){}):遍历数组返回一个加工之后的数组,返回加工之后的值

Array.prototype.filter(function(item,index){}):遍历过滤一个新的数组,返回条件为true的值

例子:

        var arr = [5,4,8,6,9,4,5,8,2,6,4,1,0];
        console.log(arr.indexOf(5));
        console.log(arr.lastIndexOf(5));
        arr.forEach(function(item,index){
            console.log('下标:'+index+','+'值:'+item);
        });
        console.log(arr.map(function(item,index){//返回一个将原数组中的值加10后的新数组
            return item+10;
        }));
        console.log(arr.filter(function(item,index){//返回原数组中大于5的数为一个新的数组
            return item>5;
        }));

猜你喜欢

转载自www.cnblogs.com/maycpou/p/12321271.html