简体   繁体   English

如何检查一个对象是否存在于另一个对象中?

[英]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.

相关问题 如何检查在另一个对象中找到的对象的已知索引的值是否存在 - how to check if a value of a known index of an object found in another object exist 如何通过jQuery在另一个数组对象中检查一组对象 - How to check a set of object is exist in another array object by jQuery 如何在javascript中使用另一个数组中的属性来过滤一个数组中的对象? - How to filter object in one array using propery in another in javascript? 如何在另一个 object 中检查至少一个 object - How to check for at least one object inside another object Angular - 循环检查 object 中是否存在值 - Angular - Loop check if value exist inside object 检查一个HTML对象是否在另一个HTML对象内 - Check if an HTML object is inside another HTML object JavaScript 检查一个 object 属性值是否存在于另一个 object 属性上 - JavaScript check if one object property value is exist on another object property JavaScript 如何检查一个 JS 对象的所有键和值是否存在于另一个 JS 对象中? - How to check whether all the key & value of a JS Object exist in the another JS object in JavaScript? 如何检查另一个对象内的对象上是否存在值? - How to check if a value is present on a obect inside another object? 如何检查我的数组的值是否存在于另一个 object 中? - How to check if a value of my array exists inside another object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM