存在于 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 潮汕话新人祝福语简短
18 退休前辈文案祝福语简短