存在于 Array 类的原型对象上的 JavaScript 函数
问题
我们需要编写一个基于Array类的原型对象的JavaScript函数。这个函数应该接受一个回调函数,这个函数应该返回第一个回调函数为真的元素。
我们应该将当前元素和当前索引作为第一个和第二个参数提供给回调函数。
示例
以下是代码-
const arr = [4, 67, 24, 87, 15, 78, 3];
Array.prototype.customFind = function(callback){
for(let i = 0; i < this.length; i++){
const el = this[i];
if(callback(el, i)){
return el;
};
continue;
};
return undefined;
};
console.log(arr.customFind(el => el % 5 === 0));输出结果15
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短