I have an array of objects (comments added by users):
const myArray = [
{
"text": "Hello",
"id": "11",
"$type": "Comment"
},
{
"text": "Date of birth: 01-01-2022",
"id": "22",
"$type": "Comment"
},
{
"text": "lalala",
"id": "33",
"$type": "Comment"
}
];
My goal is:
- find object, which string field
text
includes a phraseDate of birth
- find value of
id
field in this object
I tried this:
let dateOfBirthComment = myArray.find(element => element.text.includes('Date of birth'));
let dateOfBirthCommentId = dateOfBirthComment.id;
console.log(dateOfBirthCommentId);
When executing code above I get an error:
Error: Cannot read properties of null (reading 'includes')
Unfortunately, I can't use this:
let dateOfBirthComment = array.find(element => element.text === 'Date of birth: 01-01-2022');
Because date can be absolutely random in different cases.
Solution seems to be easy, but I'm quite new in JS and I'm really stuck. Could you please help me?