02-js实现find&&findIndx

let arr = [
  { id: 1, name: "猪八戒", gender: "男", hobbies: "吃, 喝, 睡" },
  { id: 2, name: "孙悟空", gender: "男", hobbies: "打架" },
  { id: 3, name: "白骨精", gender: "女", hobbies: "跳, rap" },
]

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

let obj = arr.myFind(item => item.id = 1)
console.log(obj)
image.png

你可能感兴趣的:(02-js实现find&&findIndx)