关于javascript:JS数组迭代方法重构everysomefilter-mapforEach

Array.prototype.every()

every() 办法用于检测数组所有元素是否都合乎指定条件(通过函数提供)。

every() 办法应用指定函数检测数组中的所有元素:

  • 如果数组中检测到有一个元素不满足,则整个表达式返回 false ,且残余的元素不会再进行检测
  • 如果所有元素都满足条件,则返回 true。

留神: every() 不会对空数组进行检测。

留神: every() 不会扭转原始数组。

//every() 办法用于检测数组所有元素是否都合乎指定条件(通过函数提供
Array.prototype.myEvery =function(fun,obj) {  //fun    obj  
    console.log(obj,'obj');
    for(i = 0;i < this.length;i++){
        // fun.bind(obj)(this[i]) 有第二个参数就扭转this指向
        if(!(obj?fun.bind(obj)(this[i]) : fun(this[i]))) {   //fun.bind(obj)(this[i])为false
            //bind() 会把obj和this[i]转换成函数并扭转他们的指向  均可调用匿名函数myEvery()
            return false;
        }
    }
    return true;
}

var arr = [1,2,3,4];
var result = arr.myEvery(function(item){  //回调函数
    console.log(this,'this');   //指向全局对象         //{ name: 'zhang' }
    return item > 1;//return false  //短路准则   1 > 1  不大于 短路  进行检测数组
},{name:'zhang'})  //扭转this指向 指向第二个参数 

console.log(result,'result');  //false

Array.prototype.some()

some() 办法用于检测数组中的元素是否满足指定条件(函数提供)。

some() 办法会顺次执行数组的每个元素:

  • 如果有一个元素满足条件,则表达式返回true , 残余的元素不会再执行检测。
  • 如果没有满足条件的元素,则返回false。

留神: some() 不会对空数组进行检测。

留神: some() 不会扭转原始数组。

// some() 办法用于检测数组中的元素是否满足指定条件(函数提供)
//如果有一个元素满足条件,则表达式返回*true* , 残余的元素不会再执行检测
Array.prototype.mySome =function(fun,obj) {  //fun    obj  
    for(i = 0;i < this.length;i++){
        // fun.bind(obj)(this[i]) 有第二个参数就扭转this指向
        if((obj?fun.bind(obj)(this[i]) : fun(this[i]))) {
            return true;
        }
    }
    return false;
}

var arr = [1,2,3,4];
var result = arr.mySome(function(item){  //回调函数
    console.log(this);   //指向全局对象
    return item > 1;  //短路准则   1 > 1  不大于 短路     // 2 > 1 满足条件 打印输出为真  进行检测数组
},{name:'zhang'})  //扭转this指向 指向第二个参数

console.log(result);  //true  //数组第二个元素满足条件 返回true

Array.prototype.filter()

filter() 办法创立一个新的数组,新数组中的元素是通过查看指定数组中符合条件的所有元素。

留神: filter() 不会对空数组进行检测。

留神: filter() 不会扭转原始数组。

 // filter() 办法创立一个新的数组,新数组中的元素是通过查看指定数组中符合条件的所有元素
//主函数
Array.prototype.myFilter = function (fun,obj) {
    // 创立一个新的数组
    var result = [];
    for(i = 0;i<this.length;i++){
        if(obj?fun.bind(obj)(this[i]) : fun(this[i])){
            result.push(this[i]);
        }
    }
    return result;
}
var arr = [1,2,3,4,5];
// 子函数
var result = arr.myFilter(function(item){  //回调函数
    console.log(this);   //指向全局对象     //打印了五次{ name: 'zhang' }
    return item > 2;  // 数组索引值累加
},{name:'zhang'})  //扭转this指向 指向第二个参数

console.log(result);  //[ 2, 3, 4, 5 ]        //返回一个新数组,数组中的元素为原始数组元素调用函数解决后的值



Array.prototype.map()

map() 办法返回一个新数组,数组中的元素为原始数组元素调用函数解决后的值。

map() 办法依照原始数组元素程序顺次解决元素。

留神: map() 不会对空数组进行检测。

留神: map() 不会扭转原始数组。

// map() 办法返回一个新数组,数组中的元素为原始数组元素调用函数解决后的值
Array.prototype.myMap = function (fun,obj) {
    var result = [];
    for(i = 0;i<this.length;i++){
        result.push(obj?fun.bind(obj)(this[i]) : fun(this[i]))  //fun(this[i])调用  return item+1
    }
    return result;
}
var arr = [1,2,3,4];
var result = arr.myMap(function(item){  //回调函数
    console.log(this);   //指向全局对象     //打印了四次{ name: 'zhang' }
    return item+1;  // 数组索引值累加
},{name:'zhang'})  //扭转this指向 指向第二个参数

console.log(result);  //[ 2, 3, 4, 5 ]        //返回一个新数组,数组中的元素为原始数组元素调用函数解决后的值

Array.prototype.forEach()

forEach() 办法用于调用数组的每个元素,并将元素传递给回调函数。

留神: forEach() 对于空数组是不会执行回调函数的。

Array.prototype.myforEach = function (fun) {
     for(var i = 0;i < this.length;i++){
          //调用参数
          fun(this[i],i,this)
     }
}
var arr = [1,4,2,9];
console.log(arr);   // [ 1, 4, 2, 9 ]
var result = arr.myforEach(function(item, index, arr){
    console.log(item, index, arr); //数组元素   数组元素索引下标   数组
//     1 0 [ 1, 4, 2, 9 ]
//     4 1 [ 1, 4, 2, 9 ]
//     2 2 [ 1, 4, 2, 9 ]
//     9 3 [ 1, 4, 2, 9 ]
}); 
 

今天要考试,知识点好多好碎。。。。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理