In-case if someone wants a little dynamic search.
let searchInArray=(searchQuery, array, objectKey=null)=>{
return array.filter(d=>{
let data =objectKey? d[objectKey] : d //Incase If It's Array Of Objects.
let dataWords= typeof data=="string" && data?.split(" ")?.map(b=>b&&b.toLowerCase().trim()).filter(b=>b)
let searchWords = typeof searchQuery=="string"&&searchQuery?.split(" ").map(b=>b&&b.toLowerCase().trim()).filter(b=>b)
let matchingWords = searchWords.filter(word=>dataWords.includes(word))
return matchingWords.length
})
}
For an Array of strings:
let arrayOfStr = [
"Search for words",
"inside an array",
"dynamic searching",
"match rate 90%"
]
searchInArray("dynamic search", arrayOfStr)
//Results: [ "Search for words", "dynamic searching" ]
For an Array of Objects:
let arrayOfObject = [
{
"address": "Karachi Pakistan"
},
{
"address": "UK London"
},
{
"address": "Pakistan Lahore"
}
]
searchInArray("Pakistan", arrayOfObject,"address")
//Results: [ { "address": "Karachi Pakistan" }, { "address": "Pakistan Lahore" } ]