版权声明:未经本人同意不得私自转载 https://blog.csdn.net/qq_40190624/article/details/82533367
find()函数用来查找目标元素,找到就返回该元素,找不到返回undefined。
如遇到重复的值只会返回第一次出现的值
var users = [
{
name:"alie"
},
{
name:"Jili"
},{
name:"carrie"
}
];
var user = [];
user = users.find(function(user){
return user.name === "carrie"
})
console.log(user)//{name: "carrie"}
// 需求,一个对象数组A,根据指定对象的条件找到数组中符合条件的对象
var posts =[
{id:1,title:"Node.js"},
{id:2,title:"React.js"}
];
var comment = {postsId:1,content:"Hello World!"};
function postForComment(posts,comment){
return posts.find(function(post){
return post.id === comment.postsId;
})
}
console.log(postForComment(posts,comment))//find.js:27 {id: 1, title: "Node.js"}