Javascript 从列表中返回对象的更简洁的方法

Javascript 从列表中返回对象的更简洁的方法,javascript,reactjs,lodash,Javascript,Reactjs,Lodash,我目前这样做是为了从数组返回一个用户对象 const userIndex = users.findIndex((user) => { return user.id === source.userId; }); const user = users[userIndex]; 有没有更干净的方法 这是一个reactjs应用程序,因此如果需要,我可以选择使用任何花式库。我的软件包中有lodash,以备不时之需。使用函数的更干净方法: const user = users.find(({id})

我目前这样做是为了从数组返回一个用户对象

const userIndex = users.findIndex((user) => { return user.id === source.userId; });
const user = users[userIndex];
有没有更干净的方法


这是一个reactjs应用程序,因此如果需要,我可以选择使用任何花式库。我的软件包中有lodash,以备不时之需。

使用函数的更干净方法

const user = users.find(({id}) => id === source.userId);
范例

const用户=[{
id:2,
名称:“Ele”
}];
常量源={
用户ID:2
};
const user=users.find(({id})=>id==source.userId);
console.log(用户)

.as控制台包装{最大高度:100%!重要;顶部:0;}
使用函数的更干净方法

const user = users.find(({id}) => id === source.userId);
范例

const用户=[{
id:2,
名称:“Ele”
}];
常量源={
用户ID:2
};
const user=users.find(({id})=>id==source.userId);
console.log(用户)
作为控制台包装{max height:100%!important;top:0;}
为什么不直接使用
.find()
而不是
.findIndex()
const user=users.find({id}=>id==source.userId)
为什么不直接使用
.find()
而不是
.findIndex()
const user=users.find({.id})=>id==source.userId)