_.find
Returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.
- // Underscore/Lodash
- var users = [
- { 'user': 'barney', 'age': 36, 'active': true },
- { 'user': 'fred', 'age': 40, 'active': false },
- { 'user': 'pebbles', 'age': 1, 'active': true }
- ]
- _.find(users, function (o) { return o.age < 40; })
- // output: object for 'barney'
- // Native
- var users = [
- { 'user': 'barney', 'age': 36, 'active': true },
- { 'user': 'fred', 'age': 40, 'active': false },
- { 'user': 'pebbles', 'age': 1, 'active': true }
- ]
- users.find(function (o) { return o.age < 40; })
- // output: object for 'barney'
Browser Support for Array.prototype.find()
45.0 ✔ | ✔ | 25.0 ✔ | ✖ | 32.0 ✔ | 7.1 ✔ |
当前内容版权归 you-dont-need 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 you-dont-need .