JS 中的 find 和 findIndex原理
2019-04-10 本文已影响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
}