美文网首页
javascript数组方法(find,some,every..

javascript数组方法(find,some,every..

作者: DeadMoon | 来源:发表于2022-11-18 11:36 被阅读0次
// forEach
Array.prototype.forEach = function(fn, context) {
  if(Object.prototype.toString.call(fn) !== "[object Function]") {
      throw new TypeError(fn + "is not a function");
  }
  for(let i = 0; i < this.length; i++) {
      fn.call(context, this[i], i, this);
  }
}
// map
Array.prototype.map = function (func, ctx) {
  let len = this.length
  if(Object.prototype.toString.call(func) !== "[object Function]") {
    throw new TypeError(func + "is not a function")
  }
  const arr = []
  for (let i = 0; i < len; i++) {
    arr.push(func.call(ctx, this[i], this))
  }
  return arr
}
// find
Array.prototype.find = function (func, ctx) {
  let len = this.length
  if(Object.prototype.toString.call(func) !== "[object Function]") {
    throw new TypeError(func + "is not a function")
  }
  let obj = undefined
  for (let i = 0; i < len; i++) {
    if (func.call(ctx, this[i], i)) {
      obj = this[i]
    }
  }
  return obj
}
// findIndex
Array.prototype.findIndex = function (func, ctx) {
  let len = this.length
  if(Object.prototype.toString.call(func) !== "[object Function]") {
    throw new TypeError(func + "is not a function")
  }
  let index = undefined
  for (let i = 0; i < len; i++) {
    if (func.call(ctx, this[i], i)) {
      index = i
    }
  }
  return index
}
// filter
 Array.prototype.filter = function (func, ctx) {
  let len = this.length
  if(Object.prototype.toString.call(func) !== "[object Function]") {
    throw new TypeError(func + "is not a function")
  }
  const arr = []
  for (let i = 0; i < len; i++) {
    if (func.call(ctx, this[i], i)) {
      arr.push(this[i])
    }
  }
  return arr
 }
// some
Array.prototype.some = function (func, ctx) {
  let len = this.length
  if(Object.prototype.toString.call(func) !== "[object Function]") {
    throw new TypeError(func + "is not a function")
  }
  let flag = false
  for (let i = 0; i < len; i++) {
    if (func.call(ctx, this[i], i)) {
      flag = true
    }
  }
  return flag
 }
// every
Array.prototype.every = function (func, ctx) {
  let len = this.length
  if(Object.prototype.toString.call(func) !== "[object Function]") {
    throw new TypeError(func + "is not a function")
  }
  let flag = true
  for (let i = 0; i < len; i++) {
    if (func.call(ctx, this[i], i)) {
      flag = false
    }
  }
  return flag
 }

附本人 git 链接

相关文章

网友评论

      本文标题:javascript数组方法(find,some,every..

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