数组方法之find

中文方法名:找对象

作用:在对象数组中找想要的对象。

1 let result = arr.find(function(item, index, array) {
2   // 如果返回 true,则返回 item 并停止迭代
3   // 对于假值(falsy)的情况,则返回 undefined
4 });

把元素作为参数传入函数之中,函数会返回一个值。

如果返回true,表示当前元素就是我们要找的。

如果返回undefined,表示当前元素不是我们要找的。

1 let users = [
2   {id: 1, name: "John"},
3   {id: 2, name: "Pete"},
4   {id: 3, name: "Mary"}
5 ];
6 
7 let user = users.find(item => item.id == 1);
8 
9 alert(user.name); // John
原文地址:https://www.cnblogs.com/flyover/p/14128636.html