课程: JavaScript: Arrays

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Find a single element with find and findIndex

Find a single element with find and findIndex - JavaScript教程

课程: JavaScript: Arrays

Find a single element with find and findIndex

- [Narrator] The array methods find and findIndex are used when you only need to return a single value within an array. These methods iterate over an array element by element returning the first element or index that passes the test. If you need to return multiple values that meet a specific criteria, you would reach for the filter array method that we will discuss later on in the course. The array methods find and findIndex operate identically but return different values. Both array methods return the first element that satisfies the criteria provided and have identical syntax. Find will return the first element that matches the testing function. And if nothing matches the criteria or the array is empty, undefined will be returned. FindIndex will return the index of the first element that matches the testing function, and if nothing matches the criteria or the array is empty, negative one will be returned. These two…

内容