I have a list object which I load from a web service. My list comes in this form:
list = [ {id:"name1 Surname1" , nb:120} ,
{id:"name4 Surname4" , nb:120} ,
{id:"name2", nb:632} ,
{id:"name3Surname3", nb:102} ,
{id:"surname4", nb:896}
]
My searching strategy is to go by "id". As you can see I have different formats:
separated name-surname with space, only name, only surname.
My purpose is to search member by id (its name or surname values). My result should be items whose IDS are containing my searched value item , even if name and surname inversed : the ordern of names and surnbames may be changefull
For example :
- If I search for : "name1" -> result : name1 Surname1
- If I search for : "surname4 name4" -> result : id:"name4 Surname4" , nb:120
- If I search for : "name1 Surn" -> result : name1 Surname1
- If I search for : "Surn name1" -> result : name1 Surname1
- If I search for : "name1 SurAAA" -> result : (nothing)
- If I search for : "AAAA Surname1" -> result : (nothing)
- If I search for : "name1Surname1" -> result : (nothing)
- If I search for : "name" -> result : name1 Surname1 , name2 , name3Surname3 , surname4
To summarize :
I should search items:
- corresponding for my searched item
- beginning with my searched item
I have tried the RegExp solution, like the following:
findPhones(written) {
this.splits = written.split(' ');
if (this.splits[0] === "") // Case of empty string
{
return this.filteredPhones = [];
}
this.filteredResult = List.filter((item) => {
const regex = new RegExp(written, 'gi'); // g : global & i: insentisive case
return item.List.match(regex);
});
But this solution is fine , but it lacks the condition of inversed "name surname" as it may be inversed :
Ex: when i search for "surname4 name4" : i get 0 result ; but in reality i should catsh the second object of my list as a good result :
{id:"name4 Surname4" , nb:120}
(it seems that i should combine two filter actions )
Any suggestions to fix it ?