Array.find+箭头函数

昨天看到一段代码,是这样的:

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,而没有参数,想知道这段代码到底是如何实现的呢?请诸大神帮解惑


湖上湖
浏览 791回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript