js数组实例方法:forEach,includes,indexOf

Array.prototype.forEach()

  • forEach() 方法对数组的每个元素执行一次给定的函数
  • 语法
    • forEach(callbackFn)
    • forEach(callbackFn, thisArg)
  • 参数
    • callbackFn
      • element:数组中正在处理的当前元素
      • index:数组中正在处理的当前元素的索引
      • array:调用了 forEach() 的数组本身
    • thisArg:执行 callbackFn 时用作 this 的值
  • 返回值:undefined
  • Array.prototype.myForEach
Array.prototype.myForEach = function (callbackFn, thisArg) {
  if (typeof callbackFn !== 'function') {
    throw new TypeError(`${typeof callbackFn} is not a function`)
  }
  const len = this.length
  for (let i = 0; i < len; i++) {
    // 稀疏数组中空槽不进行处理
    if (this.hasOwnProperty(i)) {
      // 不能提前终止函数 不支持异步函数
      callbackFn.call(thisArg, this[i], i, this)
    }
  }
}

Array.prototype.includes()

  • includes() 方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回 false
  • 语法
    • includes(searchElement)
    • includes(searchElement, fromIndex)
  • 参数
    • searchElement:需要查找的值
    • fromIndex:开始搜索的索引(从零开始),会转换为整数
      • 负索引从数组末尾开始计数——如果 fromIndex < 0,那么实际使用的是 fromIndex + array.length。然而在这种情况下,数组仍然从前往后进行搜索
      • 如果 fromIndex < -array.length 或者省略 fromIndex,则使用 0,这将导致整个数组被搜索
      • 如果 fromIndex >= array.length,则不会搜索数组并返回 false
  • 返回值:一个布尔值,如果在数组中(或者在 fromIndex 所指示的数组部分中,如果指定 fromIndex 的话)找到 searchElement 值,则该值为 true
  • Array.prototype.myIncludes
Array.prototype.myIncludes = function (searchElement, fromIndex) {
  let len = this.length
  switch (true) {
    case fromIndex > -len && fromIndex < 0:
      fromIndex = fromIndex + len
      break
    case fromIndex < -len || fromIndex === undefined:
      fromIndex = 0
      break
  }
  let i = fromIndex
  while (i < len) {
    if (
      this[i] === searchElement ||
      (Number.isNaN(this[i]) && Number.isNaN(searchElement))
    ) {
      return true
    }
    i++
  }
  return false
}

Array.prototype.indexOf()

  • indexOf() 方法返回数组中第一次出现给定元素的下标,如果不存在则返回 -1
  • 语法
    • indexOf(searchElement)
    • indexOf(searchElement, fromIndex)
  • 参数
    • searchElement:数组中要查找的元素
    • fromIndex:开始搜索的索引(从零开始),会转换为整数
      • 负索引从数组末尾开始计数——如果 fromIndex < 0,那么实际使用的是 fromIndex + array.length。然而在这种情况下,数组仍然从前往后进行搜索。
      • 如果 fromIndex < -array.length 或者省略 fromIndex,则使用 0,这将导致整个数组被搜索。
      • 如果 fromIndex >= array.length,则不会搜索数组并返回 -1
  • 返回值:首个被找到的元素在数组中的索引位置; 若没有找到则返回 -1
  • Array.prototype.myIndexOf
Array.prototype.myIndexOf = function (searchElement, fromIndex) {
  const len = this.length
  // 如果fromIndex在-len到0之间,使用fromIndex+len作为fromIndex
  // 如果fromIndex小于-len或者fromIndex为undefined,则使用0作为fromIndex
  switch (true) {
    case fromIndex < 0 && fromIndex >= -len:
      fromIndex = len + fromIndex
      break
    case fromIndex < -len || fromIndex === undefined:
      fromIndex = 0
      break
  }

  while (fromIndex < len) {
    if (this.hasOwnProperty(fromIndex) && this[fromIndex] === searchElement) {
      return fromIndex
    }
    fromIndex++
  }
  return -1
}

posted on   shenhf  阅读(6)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示