I have four arrays of the same object
which I use for a form.
They work as pairs such as list_1
contains all possible values and list_1_filtered
contains the choosen values, and the same goes with list_2
and list_2_filtered
.
The thing is that they can share similar objects with the same object.id
and I want to filter it to not add values in list_2_filtered
if it has the same object.id
as any values in list_1_filtered
.
So I did this :
getList2(form_field_value:string){
return this.list_2.filter(
(e: /*Object*/)=>
e.name.indexOf(form_field_value) != -1 &&
this.list_2_filtered.indexOf(e)==-1 &&
this.checkList2NotInList1(e)
)
}
checkList2NotInList1(object: /*Object*/): boolean {
for (let i = 0; i < this.list_1_filtered.length; i++) {
if (object.id== this.list_1_filtered.at(i).id) {
return false;
}
}
return true;
}
But I find it quite unpleasant to use another function to filter based on field
in my object
.
Is there a better way to do it ?
/!\ Names are changed for better understanding - please do not judge naming /!\
UPDATE :
class Object :
{
id: string,
itemName : string,
quantity : number
}
Data in List 1 :
id | itemName | quantity |
---|---|---|
J254 | corn | 154 |
I465 | wheat | 169 |
O25845 | strawberry | 200 |
J365 | corn | 205 |
Data in List 1 filtered :
id | itemName | quantity |
---|---|---|
J254 | corn | 154 |
Those works for a tag
field in the form such as list_1_filtered
contains the selected tags and list_1
contains all possible tags.
Same goes for list_2
& list_2_filtered
Data in List 2 :
id | itemName | quantity |
---|---|---|
J254 | corn | 154 |
P48612 | Pasta | 3010 |
E754 | egg | 312 |
J365 | corn | 205 |
Data in List 2 filtered :
id | itemName | quantity |
---|---|---|
P48612 | Pasta | 3010 |
getList2
is used when the user
writes in the input field to return suggestions. But a tag can't be in list_2_filtered
if its already in list_1_filtered
. So getList2
shouldn't return {id:J254,itemName:corn,quantity:154}
if the user writes "J2" in the input field because {id:J254,itemName:corn,quantity:154}
is already in list_1_filtered