I am developing a program where a user will pass an argument and the program will give almost the same match of the result that it will find in an array of Object. So I have an array of object which is coming from a database so when my user will pass a name the search criteria on the array of object should work something like this suppose the user have passed a name called Jack Ma now on my array there are following objects like
[
{
name: "Jacky Ma"
},
{
name: "Jacky Man"
},
{
name: "Jack Mun"
},
{
name: "Jacob Ma"
},
{
name: "Jack Ma"
}
]
so for this following case Jacky Ma, Jack Mun, Jacob Ma should get retuned in an array of object. Only thing the program should check is the spelling of the each words matches with spelling of the words that are in the object lowerCase upperCase do not matter here for both words or if there is one or more than 2. The only thing it should check is the spelling of the words if the first word spelling matches with the spelling of the first word or 2nd word spelling matches with 2nd word of the object only then it should return. If neither or one do not get matched it should return empty array.
can anyone help me how can I do this ? Thanks in Advance.
In your current code, you are comparing the whole String "Jack Ma" and not "Jack" and "Ma". You need to split the name property and the string, loop over them and compare:
const filterData = searchClan.items.filter(
n => {
const splittedName = n.name.toLowerCase().split(' ');
const splittedArgs = validateArgs.toLowerCase().split(' ');
return splittedArgs.some((argument, index) => splittedName[index] && splittedName[index] === argument)
});
The "some" method returns true if one of the element (at index "index" ) in the splittedArgs array matches with the element at index "index" in the splittedName array.