Home > Blockchain >  How to perform regex in find function of javascript?
How to perform regex in find function of javascript?

Time:11-29

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?

CodePudding user response:

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)

  • Related