Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to perform regex in find function of javascript?

This is my Object:

let temp = [{
    "title": "Student Details",
    "education": "under graduate",
    "studentId": "xyz202267"
},
{
    "title": "Student details",
    "education": "under graduate",
    "studentId": "xyz202234"
}]

I want to compare the input title with title in temp.
I want to do something like this:

let input = 'studentdetails'
const ans = temp.find(obj => obj.title.replace(/ /g,'').toLowerCase() === input)

I know this won’t work because we are trying to replace the orignal Object during run time. Is there something I can do to make this work?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your code is working,but in order to get all the results that meets the requirements,you need to use Array.filter() instead of Array.find()

let temp = [{
    "title": "Student Details",
    "education": "under graduate",
    "studentId": "xyz202267"
},
{
    "title": "Student details",
    "education": "under graduate",
    "studentId": "xyz202234"
}]


let input = 'studentdetails'
const ans = temp.filter(obj => obj.title.replace(/ /g,'').toLowerCase() === input)
console.log(ans)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading