美文网首页JavaScript
ES5/ES6中的数组方法汇总

ES5/ES6中的数组方法汇总

作者: 埼玉的头发 | 来源:发表于2020-11-11 17:46 被阅读0次

查找数组索引值

arr.indexOf(item), 数组从左向右查找

  • item 某项数组值
// 自已实现
Array.prototype.myIndexOf = function(item) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] === item) {
                 return i;
         }
     }
    return -1;
}

arr.lastIndexOf(item),数组从右向左查找

  • item 某项数组值
Array.prototype.myLastIndexOf = function(item){
    for (var i = this.length - 1; i >= 0; i--) {
        if (this[i] === item) {
            return i
        }
    }
    return -1;
}

遍历数组

arr.forEach(function(item, index, arr){})

  • fn() 回调函数
    • item 值
    • index 索引值
    • arr 调用该方法的数组
Array.prototype.myForEach = function(fn) {
    for (var i = 0; i < this.length; i++) {
        fn(this[i], i, this)
    }
}

映射数组

arr.map(function(item, index, arr){ return item}) (对原数组的加工)

  • fn() 回调函数
    • item 值
    • index 索引值
    • arr 调用该方法的数组
  • 表达式或判断式
    调用函数时书写,由用户自己定义
Array.prototype.myMap = function(fn) {
    var res = [];
    for (var i = 0; i < this.length; i++) {
          res.push(fn(this[i], i, arr))
    }
    //返回一个新数组
    return res;
}

过滤数组

arr.filter(function(item, index, arr){ return 表达式}) (对原数组的过滤)

  • fn() 回调函数
    • item 值
    • index 索引值
    • arr 调用该方法的数组
  • 表达式或判断式
    调用函数时书写,由用户自己定义
Array.prototype.myFilter = function(fn) {
      var res = [];
      for(var i = 0; i < this.length; i++) {
          if(fn(this[i], i, this)) {
                res.push(this[i]);
           }
       }
      // 返回一个新数组
      return res;
}

填充数组

arr.filter(value))

  • value 要填充的值,一般用来填充空数组
Array.prototype.myFill = function(msg) {
    for (var i = 0; i < this.length; i++) {
        this[i] = msg
    }
    return this
 }

适合由内向外运算,如求数组累加和

arr.reduce(function(pre, item, index, arr){}), firstPre)

  • fn() 回调函数
    • item 值
    • index 索引值
    • arr 调用该方法的数组
  • firstPre (可选)
    若没传,内部循环从第二个成员开始,它将作为回调函数第一次执行的第一个参数
    若传了,内部循环从第一个成员开始,它将作为回调函数第一次执行的第一个参数
Array.prototype.myReduce = function(fn, firstPre) {
    var isHave = firstPre === undefined;
    firstPre = isHave ? this[0] : firstPre;
    for(var i = isHave ? 1 : 0; i < this.length; i++) {
      //回调函数的结果作为下一个回调函数执行时的firstPre参数,因为内部在不断循环执行着回调函数
      firstPre = fn(firstPre, this[i], i, arr)
    }
    return firstPre;
}

判断数组内是否有满足条件的值

arr.some(function(pre, item, index, arr){return boolean}

  • fn() 回调函数
    • item 值
    • index 索引值
    • arr 调用该方法的数组
  • boolean
    若为true,则存在满足条件的值,立即终止内部循环
    若为false,则没有满足条件的值
Array.prototype.mySome = function(fn) {
      for (var i = 0; i < this.length; i++) {
          if(fn(this[i], i, this)){
              return true;
          }
      }
      return false
 }

判断数组内是否有不满足条件的值

arr.every(function(pre, item, index, arr){return boolean}

  • fn() 回调函数
    • item 值
    • index 索引值
    • arr 调用该方法的数组
  • boolean
    若为false,则存在不满足条件的值,立即终止内部循环
    若为true,则没有不满足条件的值(值全部满足条件)
Array.prototype.myEvery = function(fn) {
      for (var i = 0; i < this.length; i++) {
          if(!fn(this[i], i, this)){
              return false;
          }
      }
      return true;
}

小练习

实现某个区间的数字和,如1,99之间所有数字的和,不使用原生for循环.
function addNum(num1, num2) {
         var max = Math.max(num1, num2);
         var min = Math.min(num1, num2);
         console.log(new Array(5)); 
         return  new Array(num2 - num1 + 1)
                 .fill(0)
                 .map( (item, index) => index + num1)
                 .reduce( (pre, item) =>  pre + item)
 }
console.log(addNum(1, 99));

相关文章

网友评论

    本文标题:ES5/ES6中的数组方法汇总

    本文链接:https://www.haomeiwen.com/subject/laklyktx.html