find 和 findIndex 方法原理

var arr = [
    {id: 1, name: 'li 01'},
    {id: 2, name: 'li 02'},
    {id: 3, name: 'li 03'},
    {id: 4, name: 'li 04'},
]

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

var rel = arr.myFind(function(item, index) {
    return item.id === 3
})

console.log(rel)

 

posted on 2019-11-13 16:49  听说你比我贱  阅读(450)  评论(0编辑  收藏  举报

导航