手撕js中常见方法

array.fill

javascript 复制代码
Array.prototype.fill2 = function (value, start, end) {
  const length = this.length; // 获取数组的长度

  start = start >> 0; // 将start转换为整数
  // 如果end参数未填写,则默认为数组的长度,否则将end转换为整数
  end = typeof end === 'undefined' ? length : end >> 0;
  // 将start限制在合法范围内,最小值为0,最大值为数组的长度
  start = start >= 0 ? Math.min(start, length) : Math.max(start + length, 0);
  // 将end限制在合法范围内,最小值为0,最大值为数组的长度
  end = end >= 0 ? Math.min(end, length) : Math.max(end + length, 0);
  // 使用指定的value填充从start到end范围内的数组索引
  while (start < end) {
    this[start] = value;
    start++;
  }
  // 返回被修改后的数组
  return this;
}

这段代码实现了一个自定义的fill2方法,用于在数组中指定的范围内填充指定的值。它接受三个参数:value表示要填充的值,start表示填充的起始索引,end表示填充的结束索引(不包含在内)。

在代码中,首先获取了数组的长度。然后对start和end参数进行处理,将它们转换为整数,并进行范围限制,确保它们在合法的范围内。接下来,使用一个循环从start索引开始,逐个将数组的元素设置为指定的value值,直到达到end索引为止。最后,返回被修改后的数组。

通过使用这个自定义的fill2方法,我们可以在数组中指定的范围内快速填充指定的值。

array.map

javascript 复制代码
Array.prototype.myMap = function (callbackFn, thisArg) {
  if (this == "undefined" || this == "null") {
    throw new TypeError("can not read property 'map' of undefined or null");
  }
  if (typeof callbackFn !== "function") {
    throw `${callbackFn} is not a function`;
  }
  let O = Object(this); // 将当前数组对象转换为对象类型
  let T = thisArg; // 设置回调函数的this值
  let len = O.length >>> 0; // 获取数组的长度
  let A = new Array(len); // 创建一个新的数组,用于存储映射后的值
  for (let k = 0; k < len; k++) {
    for (k in O) {
      let KValue = O[k]; // 获取当前索引的值
      let mapValue = callbackFn.call(T, KValue, k, O); // 调用回调函数进行映射操作
      A[k] = mapValue; // 将映射后的值存储到新数组中
    }
  }
  return A; // 返回映射后的新数组
}

let arr = [1, 2, 3, 4];
let arr2 = arr.myMap(function (currentValue, index, Array) {
  console.log(currentValue, index, Array);
  return currentValue + 1;
});
console.log(arr2);
javascript 复制代码
    Array.prototype.myMap = function (callbackFn, thisArg) {
        if (this == "undefined" || this == "null") {
            throw new TypeError("can not read property 'map' of undefined or null")
        }
        if (typeof callbackFn !== "function") {
            throw `${callbackFn} is not a function`
        }
        let length = Object(this).length >>> 0
        let newArrray = []
        let i = 0
        while (i < length) {
            if (i in this) {
                newArrray.push(callbackFn.call(thisArg, this[i],i,this))
            }
            i++
        }
        return newArrray
    }

    let arr = [1,2,3,4]
    let arr2 = arr.myMap(function(currentValue,index,Array){
        console.log(currentValue,index,Array,this);
        return ++currentValue
    },{message:"🐟"})
    console.log(arr2);
相关推荐
晴空万里藏片云14 分钟前
elment Table多级表头固定列后,合计行错位显示问题解决
前端·javascript·vue.js
曦月合一14 分钟前
html中iframe标签 隐藏滚动条
前端·html·iframe
奶球不是球16 分钟前
el-button按钮的loading状态设置
前端·javascript
kidding72320 分钟前
前端VUE3的面试题
前端·typescript·compositionapi·fragment·teleport·suspense
李白同学1 小时前
【C语言】结构体内存对齐问题
c语言·开发语言
无责任此方_修行中2 小时前
每周见闻分享:杂谈AI取代程序员
javascript·资讯
黑子哥呢?2 小时前
安装Bash completion解决tab不能补全问题
开发语言·bash
青龙小码农2 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿2 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Σίσυφος19002 小时前
halcon 条形码、二维码识别、opencv识别
前端·数据库