美文网首页
JS 中的 find 和 findIndex原理

JS 中的 find 和 findIndex原理

作者: 璎珞纨澜 | 来源:发表于2019-04-10 16:08 被阅读0次
var users = [
  {id: 1, name: '一'},
  {id: 2, name: '二'},
  {id: 3, name: '三'},
  {id: 4, name: '四'},
]

Array.prototype.myFind = function (conditionFunc) {
  for (var i = 0; i < this.length; i++) {
    if (conditionFunc(this[i], i) {
      return this[i]
    }
  }
}

Array.prototype.myFindIndex = function (conditionFunc) {
  for (var i = 0; i < this.length; i++) {
    if (conditionFunc(this[i], i) {
      return i
    }
  }
}

var ret = users.myFind(function (item, index) {
  this.id === 2
}

相关文章

网友评论

      本文标题:JS 中的 find 和 findIndex原理

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