昨天看到一段代码,是这样的:const pets = [
{ type: 'Dog', name: 'Max'},
{ type: 'Cat', name: 'Karl'},
{ type: 'Dog', name: 'Tommy'},
]
function findDog(name) { for(let i = 0; i<pets.length; ++i) { if(pets[i].type === 'Dog' && pets[i].name === name) {
return pets[i];
}
}
}用短方法后:pet = pets.find(pet => pet.type ==='Dog' && pet.name === 'Tommy');
console.log(pet); // { type: 'Dog', name: 'Tommy' }我查了查arr.find方法,定义是array.find(function(currentValue, index, arr),thisValue)上面的代码在pet=pets.find()内又传入pet,而没有参数,想知道这段代码到底是如何实现的呢?请诸大神帮解惑
添加回答
举报
0/150
提交
取消