[英]How to check a propery of an object inside another object exist?
Using the following array with two objects that include another object inside I would like to know if there is a way to find which is the applicationID has applicantID equal with "5bd3480af09ddf0258c3a31d" 将以下数组与两个对象(其中包含另一个对象)一起使用时,我想知道是否有一种方法可以找到哪个applicationID的申请人ID等于“ 5bd3480af09ddf0258c3a31d”
Array [
Object {
"__typename": "JobApplication",
"_id": "5be097753397465946e051fd",
"applicant": Object {
"__typename": "User",
"_id": "5bd83b9a62a9f33cf0f1033b",
},
},
Object {
"__typename": "JobApplication",
"_id": "5bdc7c8b3241cb5bc10ac694",
"applicant": Object {
"__typename": "User",
"_id": "5bd3480af09ddf0258c3a31d",
},
},
]
So in this case it has to return "5bdc7c8b3241cb5bc10ac694". 因此,在这种情况下,它必须返回“ 5bdc7c8b3241cb5bc10ac694”。
This are my two constants first will return the user id and second will return only the applications id. 这是我的两个常量,第一个将返回用户ID,第二个将仅返回应用程序ID。
const { _id } = this.props.getMe.me._id;
const applicationID = getJobApplicationsForThisJob.map(application => application._id);
I could check if the user id is in any of the objects like this 我可以检查用户ID是否在这样的任何对象中
const applicantId = getJobApplicationsForThisJob.map(user => user.applicant._id);
const hasApplied = applicantId.includes(_id);
Thanks 谢谢
You can use the .find
method. 您可以使用.find
方法。
var item = arr.find(function(x) {
return x.applicant._id == find;
});
The find
method returns the first found item or undefined if a record was not found. find
方法返回find
的第一个项目,如果未找到记录,则返回undefined。
var arr = [{ "__typename": "JobApplication", "_id": "5be097753397465946e051fd", "applicant": { "__typename": "User", "_id": "5bd83b9a62a9f33cf0f1033b", } }, { "__typename": "JobApplication", "_id": "5bdc7c8b3241cb5bc10ac694", "applicant": { "__typename": "User", "_id": "5bd3480af09ddf0258c3a31d", }, }]; var find = "5bd3480af09ddf0258c3a31d"; var item = arr.find(function(x) { return x.applicant._id == find; }); console.log(item != undefined ? item._id : "Not Found");
Since you are using Javascript, you would want to do something like my comment, 由于您使用的是Javascript,因此您希望执行类似我的评论的操作,
let _id = "5bd3480af09ddf0258c3a31d";
let list = []; //your list of objects.
let applicationResults = list.where( (app) => app.applicant._id = _id);
if (applicationResults.length == 0){
console.log("no applicantion with this applicant found.");
}else {
console.log("Found " + applicationResults.length + " Applications from this Applicant")
return applicationResults;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.